Click here to Skip to main content
15,881,789 members
Articles / Database Development / NoSQL

Using MongoDB With Visual Studio

Rate me:
Please Sign up or sign in to vote.
3.91/5 (3 votes)
10 Feb 2010CPOL3 min read 48.2K   6   3
An article about 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 9.10 (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 otherwise I never got an IP address my host computer could access.

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

Note: 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 like the screenshot below:

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…

C#
//connect to the source (IP address of virtual)
Mongo mongo = new Mongo("100.0.0.1");
mongo.Connect();

//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
users.Insert(user);

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…

C#
use website
db.users.find() 

And I would get a response similar to this…

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 code (anybody remember that old stuff?). It is still early on in the project so if you're interested in helping, feel free to contact me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Scarsms6-Sep-12 13:13
Scarsms6-Sep-12 13:13 
GeneralAnother .NET driver for Mongo Pin
Fred Pinto17-Jun-10 22:09
Fred Pinto17-Jun-10 22:09 
GeneralThanks Pin
alex turner9-Feb-10 22:48
alex turner9-Feb-10 22:48 
Thanks for bringing this to the community. It is great to see a linux DB being contacted from C#/VS.

www.nerds-central.com - welcomes all nerds Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.