SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Shirsendu Nandi Articles | ADO.NET in C# May 04, 2011
In this article I will describe How to use C# Dictionary value to fed in to the Mongo Database.
Reader Level:
Download Files:
 


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.
 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor