Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Using MongoDB With Visual Studio

, 10 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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…

//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…

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)

Share

About the Author

webdev_hb

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 PinmemberScarsms6-Sep-12 13:13 
GeneralAnother .NET driver for Mongo PinmemberFred Pinto17-Jun-10 22:09 
GeneralThanks Pinmemberalex turner9-Feb-10 22:48 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141030.1 | Last Updated 10 Feb 2010
Article Copyright 2010 by webdev_hb
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid