Getting Started with MongoDB and .NET

Getting up and running with MongoDB using C# is a very simple process. It literally takes a couple of minutes, including the time to download MongoDB itself and the C# drivers - so if you fancy having a play around with it to see what it’s all about, there’s no barrier stopping you.

Nutshell

To quote MongoDB.org:

MongoDB (from “humongous”) is a scalable, high-performance, open source, document-oriented database.

Check out the MongoDB homepage for a highlight of the key features.

Step 1 : Download the latest MongoDB release from here

Extract the .zip file to somewhere memorable. It’s a good idea to rename the folder to just “mongodb” unless you have an amazing memory as the folder name will by default include all kinds of version info. e.g. C:\mongodb\ is as good a place as any

Step 2 : Download the latest MongoDB C# Driver from GitHub

Available as .msi or .zip file. So install or unzip as you see fit.

Step 3 : Create a data directory

By default, MongoDB stores data in \data\db - you need to create those directories yourself. Personally, I chose to put the directory under the mongodb folder - C:\mongodb\data\db\

Step 4 : Fire up the MongoDB server

In a cmd prompt, navigate to the mongodb\bin directory.
If you’re using the default data directory, you run:

mongod

If you’re using a different data directory, you need to specify that as an argument:

mongod --dbpath C:\mongodb\data\db\

So you should see something like this:

You now have MongoDB running.

Step 5 : Start coding
Create a new .NET project and add references to MongoDB.BSON.dll and MongoDB.Driver.dll from the C# driver installed in Step 2. By default, these are in program files under: MongoDB\CSharpDriver {VersionNumber}\

From this point, the C# driver tutorial is a great way to get started. Scan your eye over that and you’ll have code interacting with MongoDB in no time at all. It really is quick and easy to get going with the basics. Within minutes, you can have written code to insert, find and delete documents as well as becoming comfortable with the API and seeing the differences to that of working with a RDBMS like SQL Server.

So that’s it. A very quick, whistle-stop guide to the basics of getting started with MongoDB from a .NET point of view.


See also