MongoDB – how to get started

by DotNetNerd 3. January 2010 11:17

Even though I have been pretty disconnected during the holydays I couldn’t help read that Rob Conery wrote quite a bit on his blog about his thoughts on Ayendes “EF vs NHibernate” discussion. Rob Conery argues that he is tired of these discussions and that we need to look at the NOSQL movement and instead focus on alternatives to relational databases.

I found this pretty interesting and decided that it was finally time for me to take a look at one of the document based databases out there. As Rob mentioned MongoDB I thought this was a good a place to start as any. Based on my experiences I then decided to do this post on what I learned about getting MongoDB up and running. So first of all, the steps to get a MongoDB server running are:

  1. Download and unpack from: http://www.mongodb.org/display/DOCS/Downloads
  2. Create folder C:\data\db and make sure the server has read and write access
  3. Run the server from cmd. Go to dir where you unpacked (eg: "c:\MongoDB\bin") and run "mongod"

You can now work with the database from the commandline, but to start writing some code you need to get a driver that suits you. In my case I went with a driver so I can write my code in C#. This driver along with a early edition of a LINQ library can be downloaded from http://github.com/samus/mongodb-csharp/downloads 

This is all you need to get going, so just create a new project and start coding!

A good sample can be found here http://blog.dynamicprogrammer.com/2009/11/10/UsingMongoDBFromC.aspx and if your into F# this might be interesting http://gist.github.com/218388

The first sample focuses on two ways to handle converting objects into documents.

  1. Serialize object(hierarchy) into JSon using JSonConverter - which was the sample I focused on.
  2. Make entities implement the IMongoEntity interface – which basically just makes your entity expose a Document property which then contains the state for the entity.

Of course I wont cheat you from seeing the bit of code I started out with, which is heavily inspired by the before mentioned blog posts.

//Connect to the database
var mongo = new Mongo();
mongo.Connect();
var db = mongo.getDB("movieReviews");

//Access the collection that you wish to work with
var movies = db.GetCollection("movies");

//Add a document
var movie = new Document();
movie["title"] = "Star Wars";
movie["leadActor"] = JsonConvert.SerializeObject(
 new Actor() {Name = "Mark Hamill", Age = 26, Gender = Gender.Male});
movie["releaseDate"] = new DateTime(1977, 5, 25);
movies.Insert(movie);

//Do a query
var result = from mov in movies.AsQueryable()
             where (string)mov["title"] == "Star Wars"
             select mov;

//Write out info from document
foreach (var document in result)
 foreach (string key in document.Keys) Console.WriteLine(document[key]);

//Deserialize and write name
Console.WriteLine(JsonConvert.DeserializeObject<Actor>((string)result.First()["leadActor"]).Name);

//Disconnect from database
mongo.Disconnect();

 

You'll also need this simple class for the sample:

public class Actor
{
 public string Name { get; set; }
 public int Age { get; set; }
 public Gender Gender { get; set; }
}

public enum Gender
{
 None = 0,
 Male = 1,
 Female = 2
}

Tags: ,

Who am I?

My name is Christian Holm Diget, and I work as an independent consultant, in Denmark, where I write code, give advice on architecture and help with training. On the side I get to do a bit of speaking and help with miscellaneous community events.

Some of my primary focus areas are code quality, programming languages and using new technologies to provide value.

Microsoft Certified Professional Developer

Microsoft Most Valuable Professional

Month List

bedava tv izle