Using MongoDB With Visual Studio

I recently found out about MongoDB and decided to check it out and so far I've been really impressed. The most interesting thing about MongoDB that it doesn't work like a normal database -- there aren't really any schemas for any of the "tables". In fact, you can make changes to the structure of any record at any time without affecting the rest of the "table".

Getting Started

Here are a few steps I used to get MongoDB running and testable from my Visual Studio. To keep my main computer clean I used a virtual instance of Ubuntu to host the "database server".

This part might take a little bit of time but start by downloading VirtualBox and Ubuntu (You don't need to download Mongo just yet). Once everything is downloaded install and configure Ubuntu but don't start it up right away (you need to configure some stuff).

You're going to want to make sure that your PC can connect to the virtual instance on the standard MongoDB port (unless you change it of course). If not, open a command prompt (on the host system) and then run the following commands (from the VirtualBox directory).

VBoxManage setextradata UbuntuDev "VBoxInternal/Devices/pcnet/0/LUN#0/Config/MongoDB/HostPort" 27017 VBoxManage setextradata UbuntuDev "VBoxInternal/Devices/pcnet/0/LUN#0/Config/MongoDB/GuestPort" 27017 VBoxManage setextradata UbuntuDev "VBoxInternal/Devices/pcnet/0/LUN#0/Config/MongoDB/Protocol" TCP

This example uses the same name (UbuntuDev) that I used for the screenshot example above. Make sure you use the correct name when setting yours up.

It is also worth mentioning that I had to use a Bridged Connection for my Network connection so I'd get an IP address from my wireless. But, if you aren't on a wireless network... say, like your in-laws house for several hours... you can use Host Only Adapter so you can keep working... just sayin'...

Inside Ubuntu

Once you've successfully setup your Ubuntu system go on and download the MongoDB binaries. I don't know the correct location to install these files so I placed them inside /etc/mongodb.

If you've never used Linux before then warm up your typing fingers and open a terminal window (Applications > Accessories > Terminal). Start typing in the following commands...

sudo bash
mkdir /data
mkdir /data/db
mkdir /etc/mongodb
chmod a=rwx /etc/mongodb

This is certainly not the recommended security setup for this folder but for our testing purposes it is sufficient.

At this point we can revert back to our lazy Windows ways and drag the contents of the .tgz file into the /etc/mongodb directory. Once we have the contents copied over switch back to the terminal window and then type...

sudo /etc/mongodb/bin/mongod

And you should see something similar to the screenshot...

Once you see this message you should be ready to test from Visual Studio but you can always test it from Ubuntu by opening a new Terminal window and typing...

sudo /etc/mongodb/bin/mongo

Which allows you to enter commands and make changes to the database similar to the online demo on their website.

Connecting Via Visual Studio

I haven't found much for C# code to connect to Mongo but there is currently a project hosted on GitHub that allows you to perform queries and edit documents. It is a mess of code - but it does at least work... mostly... (nothing personal guys) ;) Here is a simple example of how to use the code...

//connect to the source (IP address of virtual)
Mongo mongo = new Mongo("");

//get the database and 'table'
Database website = mongo.getDB("website");
IMongoCollection users = website.GetCollection("users");

//make a new document of information
Document user = new Document();
user["name"] = "Hugoware";
user["age"] = 29;
user["isAdmin"] = false;

//then save the document

If you still have your Terminal window up then you might have noticed messages listed in response to your update. If you still have the MongoDB command line up you can view your changes by entering in a few commands. For example, to see the database I just created in the sample above I would enter...

use website

And I would get a response similar to the screenshot shown...

What Is Next?

Personally, I think MongoDB is going to end up being huge. The main problem I see for C# developers is that MongoDB really favors Dynamic Languages which isn't really a strong suit of the language.

Right now I'm working on my own driver to talk to Mongo that heavily relies on my AnonymousType class I wrote from before. It is still early on in the project so if you're interested in helping feel free to contact me.

February 9, 2010

Using MongoDB With Visual Studio

Post titled "Using MongoDB With Visual Studio"