How to Use a Dictionary Value in a Mongo Database Using C#.NET


Description: In this article I will describe how to use a C# Dictionary value in a Mongo Database.

Content:

In the sample application provided below I will show you how to fetch the data from a dictionary and save into the Mongo Database. The purpose of using a dictionary is to insert the un-typed or unspecified object in to the Mongo Database. Because it might be required to store the data into the database that does not have a specific type at compile time. It may be specified at runtime or the user can create any data with his own type. So it is an untyped value. For that we are using the dictionary to handle the unspecified data.

Step 1:

Create a sample application named "dictionarymongodb" (my project is VS 2010).

Step 2:

To download Mongo Database and to know what a Mongo Database is please visit this article.

This is for beginners.

Step 3:

Now go to the "bin" folder under the "mongodb-win32-i386-1.8.1" folder. After that first click the "mongod" exe just like it is the Mongo DB server.

Figure 1

MongoDb1.gif

Step 4:

Now create a Generic Dictionary and add the value like in the code shown below:

IDictionary<string, object> d = newDictionary<string, object>();
KeyValuePair<string, object> k = newKeyValuePair<string, object>("test", "test1");
KeyValuePair<string, object> k1 = newKeyValuePair<string, object>("NAme", "shirsendu");
KeyValuePair<string, object> k2 = newKeyValuePair<string, object>("Adress", "Korammangala");
KeyValuePair<string, object> k3 = newKeyValuePair<string, object>("City", "BAngalore");
d.Add(k);
d.Add(k1);
d.Add(k2);
d.Add(k3);


Step 5:

Now create the mongo server and create the collection like the following code:

var mongo = newMongo();
mongo.Connect();
var db = mongo.GetDatabase("Dictianary");
var collection = db.GetCollection<Dictionary<string, object>>("dict");
//Document dc = new Document();

Here you have noticed that I am giving the Dictionary type of the GetCollection Method.

So the Mongo Database collection will hold the Dictionary typed object data.

Step 6:

Now we have to Insert the dictionary data in to the Mongo Database. For that the code is:

collection.Save(d);

Step 7:

For displaying the dictionary data write the following code:

foreach (var pair in d)
{
Console.Write("{0}",
pair.Key
);
Console.WriteLine();
Console.Write("{0}:-",
pair.Value
);


}

Now the whole code is:

foreach (var pair in d)
            {
Console.Write("{0}",
pair.Key
               );
Console.WriteLine();
Console.Write("{0}:-",
pair.Value
               );

            }

Now the whole code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Linq;
using MongoDB;
using MongoDB.Bson;

namespace dictionarymongodb
{
public classDictoperation
    {
public void operation()
        {
IDictionary<string, object> d = newDictionary<string, object>();
KeyValuePair<string, object> k = newKeyValuePair<string, object>("test", "test1");
KeyValuePair<string, object> k1 = newKeyValuePair<string, object>("NAme", "shirsendu");
KeyValuePair<string, object> k2 = newKeyValuePair<string, object>("Adress", "Korammangala");
KeyValuePair<string, object> k3 = newKeyValuePair<string, object>("City", "BAngalore");
d.Add(k);
d.Add(k1);
d.Add(k2);
d.Add(k3);
IDictionary<string, object> d1 = newDictionary<string, object>()
       {
           {"catname", "cat"},
           {"dogname", "dog"},
           {"llamaname", "llama"},
           {"iguananame", "iguananame"}
       };

var mongo = newMongo();
mongo.Connect();
var db = mongo.GetDatabase("Dictianary");
var collection = db.GetCollection<Dictionary<string, object>>("dict");
//Document dc = new Document();

foreach (var pair in d)
            {
Console.Write("{0}",
pair.Key
               );
Console.WriteLine();
Console.Write("{0}:-",
pair.Value
               );

            }
collection.Save(d);

vartotalNumberOfPosts = collection.Count();

//varpostsThatJaneCommentedOn =
//    (from p in collection.Linq()
//     select p.Name).ToList();

mongo.Disconnect();

Console.ReadLine();
 
        }
    }
classProgram
    {
static void Main(string[] args)
        {
Dictoperationdp = newDictoperation();
dp.operation();
        }
    }
}

Conclusion:

So in this article we have seen how to add dictionary data to a Mongo Database.
 

Up Next
    Ebook Download
    View all
    Learn
    View all