How to Create a List of Documents in Mongo Database Using .NET



Description: In this article I will describe how to create a list of documents under the parent documents using C# .Net and a Mongo Database.

Content:

For what MongoDB is and how to install it and how to install the C# MongoDB driver then please see this article:

http://www.c-sharpcorner.com/UploadFile/b19d5a/6716/

Now we are creating the sample project.

Now go to the "bin" folder under the "mongodb-win32-i386-1.8.1" folder. After that click the "mongod" exe just like the Figure below; it is the server of the mongo db. You must open the mongo database server while you are creating some application using the mongo db. We can open the mongo database server by using the windows service also.

Mongo1.gif

Step 1:

Create a new Console application named "mongodbconsoleapplication".

Step 2:

Now you have to add a reference for "mongodriver.dll" to your project. For that right click the Reference folder and click "add reference" just like
Figure 1.

Figure 1:

Mongo2.gif

Step 3: Now from the "Browse" tab select the required dll and press ok just like Figure 2. I have attached all the dll in my sample application.

Figure 2:

Mongo3.gif

Step 4:

Create a new class named "Mongoeg3" under the name space.

Step 5:

Paste the code shown below under the Mongoeg3 class:

public void operation()
        {
            var mongo = new Mongo();

            mongo.Connect();

            var northwind = mongo.getDB("Employee");

            var categories = northwind.GetCollection("Employeedetails");
 
            var category = new Document() { { "Title", "shirsendu nandi" } };
            category["Id"] = "11541";
 
            var Locations = new List<Document>() //add the multiple documents
 
                                {
 
                                    new Document() {{"Title", "Anoop"}},
 
                                    new Document() {{"Title", "Dipti"}},
 
                                    new Document() {{"Title", "sachin"}}
 
                                };

            category.Add("Locations", Locations.ToArray());//add the products in to the category

            // persist the category along with products 

            categories.Insert(category);//insert the Parent and the child documents uner the collection

            var persistedCategory = categories.FindOne(new Document() { { "Title", "shirsendu nandi" } });
            var list = northwind.GetCollection("Employeedetails").FindAll().Documents;
            Console.WriteLine(persistedCategory["Title"]);
            foreach (var lt in list)//to display the parent document
            {
                Console.WriteLine(lt["Title"]);
                Console.WriteLine(lt["Id"]);

            }
 
            foreach (var Location in (persistedCategory["Locations"] as Document[]))
            {
 
               Console.WriteLine(Location["Title"]);//display the child documents
               
            }
         
            mongo.Disconnect();
        }


The above code shows use of a list of documents under the parent document named "Title".
When you run the application and if you debug this class then you will see that the value comes like the document just like Figure 3:

Figure 3:

Mongo4.gif

Now to find the Particular Title (for eg: shirsendu Nandi) we are using this code:

var persistedCategory = categories.FindOne(new Document() { { "Title", "shirsendu nandi" } });

These will show us the Parent Document Title Name.
For getting the Child Document Title Name we have to use this code:

foreach (var Location in (persistedCategory["Locations"] as Document[]))
            {

               Console.WriteLine(Location["Title"]);//display the child documents
            }

Step 5:

Now create the object of the mongoeg3 under the void main method:

Mongoeg3 eg3 = new Mongoeg3();
            eg3.operation();

Step 6:

Now run the Program; it will look like:

Mongo5.gif

Conclusion: So in this article I have described how to create a parent and child document in a Mongo Database.
 

Up Next
    Ebook Download
    View all
    Learn
    View all