Tuesday, May 11, 2010

Programming MongoDB on Windows – Basics

Here are a few samples that build on my previous post “Installing MongoDB on a Windows Server.”  The following code snippets were developed in Visual Studio 2010 with the IronPython Tools for Visual Studio installed.  You can grab these tools here
The first code snippet queries for the one document that we inserted into the database in the previous post.  This just shows a basic connection and query using the C# programming language.  The .NET drivers for MongoDB that I am using here, mongodb-csharp, come from the following link: http://github.com/samus/mongodb-csharp.
Next is the exact same code only in IronPython.  I had one issue while creating this simple sample.  The IronPython code would not load the MongoDB.Driver.dll that I downloaded from github.  I had to download the source for it and rebuild it.  Then, IronPython would load the assembly.

Installing MongoDB on a Windows Server

To start, I downloaded the latest mongo binaries from  the mongo download site.  This was version 1.4.2 at the time of this writing.  I was installing mongo on a 64 bit Windows 2008 server so I picked the Windows 64-bit binary package.

On the server, I created the folder C:\Program Files\MongoDB to hold the binaries and the folder C:\ProgramData\mongodb\data to hold the mongo databases.  I then extracted the binaries from the downloaded file (mongodb-win32-x86_64-1.4.2.zip) to the C:\Program Files\MongoDB folder.  I then opened a command prompt as Administrator and installed mongo as a service by executing the following line:

“C:\Program Files\MongoDB\bin\mongod” -–dbpath C:\ProgramData\mongodb\data -–install

This runs the mongod.exe executable and tells it to use C:\ProgramData\mongodb\data as its database directory and to install itself as a Windows service.  It is important that you use the full path to the mongod.exe executable or else the Windows service control manager will not be able to find the executable to start the service.  The service can now be started by executing the this command from the administrative command prompt:

net start MongoDB

I then used the mongo.exe database client application to test the install.  I inserted a simple person record and then queried for that record.  Mongo creates a ‘test’ database and a ‘things’ collection on my behalf and inserts the document into it.  A screen shot is shown below.  You can see the object ID assigned to the document by mongoDB.



To access mongoDB from another computer the Windows firewall needs to be configured.  I allowed any domain connections to access this installation of mongo.  This may not be acceptable for your needs.  As this is a test database in my environment, this configuration is acceptable.  Here is a screen shot to show the configuration in Windows firewall.



Now I wanted to connect to the database from another computer.  On my own desktop I then ran the mongo.exe database client to connect to the server and the ‘test’ database.  Then I queried for the known record.  The results are shown below.


As you can see you use the <server name>/<database name> pair to connect.  There are many more options of course, but this is sufficient if you are using the defaults.