Logo
This is a very quick ‘get up and running in as few steps as possible’ because MongoDB makes it so easy to get started.

Setup MongoDB

  • Download the latest version of MongoDB from http://www.mongodb.org/downloads (v1.8.2 at time of writing)
  • Unzip the files and place the bin folder somewhere (e.g. c:\mongo\bin)
  • Create a folder c:\data\db (this is the default expected folder mongo will use)
  • Double click on mongod.exe

Congratulations you’ve just setup mongodb and started it!

Do stuff in c#

  • Create a new Console Application
  • Using Nuget (Tools > Library Package Manager > Add Library Package Reference), search for “mongocsharpdriver”, let Nuget add the references
  • Paste in this code:
    using MongoDB.Bson;
    using MongoDB.Driver;
     
    namespace MongoDbTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                // connect to server
                var mongo = MongoServer.Create();
                // create database
                var db = mongo.GetDatabase("test");
                // create collection
                var books = db.GetCollection("books");
                // create entry
                var book = new{
                                Id = ObjectId.GenerateNewId(),
                               name = "Dans Book"
                           };
                // save entry
                books.Save(book);
            }
        }
    }
  • Run the application.

Congratulations you’ve just leveraged MongoDB from .Net!

Don’t believe me?

Okay so this is about as basic an example as I could make it and of course the application starts, runs and finishes without much of a fanfare. Just to go a tiny bit deeper we’ll use the Mongo shell to look for our entry…

  • From your Mongo directory (c:\mongo\bin), double click on Mongo.exe and enter:
     db.books.find()

Voila! You should see the book you just saved using your app returned.

This obviously barely scratches the surface but does get you up and running and demonstrates just how simple MongoDB is to get up and running. Now go dig a little deeper: http://www.mongodb.org/.