Introduction to Mongo DB and C#

Summary

So I wanted to try out MongoDB and see what it was all about.  In this blog post I’m going to give a quick run-down of how to get things up and running just to show how easy it is to install and connect to a MongoDB database.


Where to Get MongoDB

The official MongoDB website is here, but you can skip right to the download page (http://www.mongodb.org/downloads) and download the windows version of MongoDB.  MongoDB is very easy to install and setup.  Just run the installer like any other windows install program, then you can go straight to the “Windows Quickstart” page (here) to get things setup.

I setup the default configuration by creating a “C:datadb” directory, then I navigated into my C:Program FilesMongoDB 2.6 Standardbin” directory and double-clicked on the mongod.exe file.  I spent some time with the mongo.exe program to manually create “collections” (mongo’s equivalent to a table), but you can skip right to the C# code if you’d like.  The MongoDB website has a great webpage for getting started with the MongoDB commands if you’re familiar with SQL.  You can go here for this page.


The C# Code

I created a console application in Visual Studio 2012 and then I went straight to NuGet and typed in MongoDB in the search box.  The “Official MongoDB C# driver” showed up on top and I installed that one.  Next, I went to the Getting Started With the C# Driver page (here) and started typing in some sample code.

One of the first things I discovered with MongoDB is that an insert will create a collection.  Also any field that is misspelled can cause a record with a different field name to be created in that collection.  

To get started, include the following using statements:

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;



Then setup a connection and open your database using these commands:


var connectionString = “mongodb://localhost“;
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase(“test“);
var collection = database.GetCollection<Users>(“users“);


These are for the standard MongoDB installation.  

Next, you’ll need an object to represent your data, similar to the way that NHibernate declares records:

public class Users
{
    public ObjectId Id { get; set; }
    public string FirstName { get; set; }
    public int Age { get; set; }
}

Now let’s insert some data just to populate a few records:

var entity = new Users { FirstName = “Joe“, Age = 27 };
collection.Insert(entity);
var id = entity.Id;


I ran the program as is and then changed the FirstName and Age and ran the program again to insert another record.  You can set up a program to insert a bunch of data if you like, I only needed a couple of records to test the LINQ capabilities.

The last part of my program looks like this:

var query =
    from e in collection.AsQueryable<Users>()
    where e.Age < 34
    select e;

foreach (var item in query)
{
    Console.WriteLine(item.FirstName);
}

Console.ReadKey();

My result, as expected, is a list of first names that were inserted into the sample database.  In a nutshell, this was a very simple example just to demonstrate the basics of getting things up and running.  In order to turn this technology into something serious, we would need to setup MongoDB on a server and make sure it runs reliably (i.e. auto-starts when the machine is rebooted).  We would also need to make sure it is secure and that we can make a connection from the machine running the C# program.  After that, it’s a matter of designing software pretty much the same as any SQL Sever or Oracle program.  Obviously the mappings are simpler than Fluent NHibernate.

My next test will be to create two collections and join them.  Then I need to test performance differences between MongoDB and NHibernate CRUD operations using a long list of names.  After that, I hope to move on to what makes MongoDB “different” and more powerful than a standard SQL server.


 

Leave a Reply