Using MongoDB via .NET

Objective :

In this post i will explain how quickly we can interact with NoSQL database using .Net.

Abstract:

NoSQL databases have gained popularity for their simpler design, convenience in usage & finer control in contrast with SQL database. They are being used for simple content management requirements like data retrieval & append, that demands key-value data structure. The whole idea is to use simple database for simpler requirements. I have used Mongodb  & Visual Studio 2010 to achieve this goal.

Setting up MongoDB :

For many developers this is going to be their first encounter with NoSQL database. Therefore i will simply suggest readers to navigate MongoDB.

Read pre-requisite & then download MongoDB database. For our demonstration purpose I have used “Mongodb-win32-i386-2.2.3″

Configure Mongodb.  Make sure you make a “Data\db” directory at drive Root where your MongoDB (Mongodb-win32-i386-2.2.3) folder . MongoDB System will store all its data under “Data\db“.

For convenience i renamed this(Mongodb-win32-i386-2.2.3)) to MongoDB only, It helps later when we use the command prompt ( a shorter version ).

Open a command line console, navigate to to MongoDb\bin  & run mongod, ( primary process to connect with MongoDB server instance )

Open a new command line console, navigate to to MongoDb\bin  & run mongo, ( a shell interface to MongoDB )

*Our shell interface should be connected to server listening on port 27017 ( default port )

some .NET code :

MongoDB Team provides Several driver to connect with MongoDB System.  Download C# driver.

Open Visual Studio 2010 , create new windows forms application.

Add project Reference to MongoDB.Driver.dll

Form1

Go to code behind & add namespaces

# region MongoRefs
 using MongoDB;
 using MongoDB.Driver;
 using MongoDB.Driver.Linq;
 using MongoDB.Driver.Builders;
 #endregion

Insert Operation :

I used following code to populate some data in our mongodb datastore.

MongoServer mongoServer = MongoServer.Create();
mongoServer.Connect();
MongoDatabase mDB = mongoServer.GetDatabase("Analytics");
MongoCollection mCollection = mDB.GetCollection<DemoStats>("DemoStats");
#region Populate manual data
if (mCollection.Count() == 0)
{
DemoStats objDemoStat = new DemoStats() { SpeakerName = "Sarfraz", Id = Guid.NewGuid().ToString(), Topic = "Demonstrating MongoDB & Twitter with Web API", AudienceCount = "11" };
mCollection.Save(objDemoStat);
objDemoStat = new DemoStats() { SpeakerName = "Asif", Id = Guid.NewGuid().ToString(), Topic = "Overview Microsoft Dynamics CRM", AudienceCount = "13" };
mCollection.Save(objDemoStat);
objDemoStat = new DemoStats() { SpeakerName = "Fahad", Id = Guid.NewGuid().ToString(), Topic = "OData Services with WCF Data Services", AudienceCount = "15" };
mCollection.Save(objDemoStat);}
#endregion

You can see via Mongo Console how our data looks like.

MongoConsole

Read Operation :

I used following code to retrieve/query database records, This code applies a filter & uses the SpeakerName Field for data retrieval.

#region Retrieve Data from Mongo

if (Convert.ToInt32(mCollection.GetStats().ObjectCount) > 0)
 {

var demoData = mCollection.FindAs(typeof(DemoStats), Query.EQ("SpeakerName", txtSpeakerName.Text));
 if (demoData.Count() > 0)
 {
 foreach (DemoStats objStat in demoData)
 {
 lblTopic.Text = Convert.ToString(objStat.Topic);
 lblAudienceCount.Text = Convert.ToString(objStat.AudienceCount);
 }

lblMessage.Text = "Match Found";
 }
 else
 {
 lblMessage.Text = "Match Not Found";
 }
 }
 else
 {
 lblMessage.Text = "No Records Found";
 }
 #endregion

In the end, make sure your code disconnects from Mongo Instance

mongoServer.Disconnect();

So this is it. A couple of simple data operations to help us get acquainted with NoSQL.

About these ads

Tagged: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: