Thursday, June 10, 2010

Practical MongoDB Part 1: Up and Running

Like many others, I've been intrigued by the NoSQL movement and the various alternatives which have appeared in recent years. One of these options which is rapidly growing in popularity is MongoDB, a document oriented database written in C++ with scalability in mind.

This post is the first in a series documenting my attempts to implement MongoDB into a real world project.

MongoDB has garnered a following in the ruby and php communities, but up until recently had little exposure to .Net folks. The earliest .Net driver, mongodb-csharp, was basically a wrapper on Mongo's built in capabilities. While useful, this design did little to provide a strongly typed approach to data access. More recently however, another open source effort led by Andrew Theken and Rob Conery created NoRM: a strongly typed driver (which even has a sweet Linq provider). There are other C# drivers out there, but these are the ones I have experienced. Part 2 and onward of this series will use NoRM.

Setting it up... For the Windows user

Hopefully you've already downloaded the mongo binaries and played around with the command line interface. If not, I recommend doing so. Plenty of others have discussed this before so I won't cover it again. This is one of the walkthroughs I used to get started.

Once you've tried out the command line interface, you'll start wondering how to get this running as a service on your server. With earlier versions of Mongo it may have been necessary to create a batch file and install a service manually to call this batch file. However, a feature was added recently (version 1.2 I believe) which performs the service installation for you. Simply call the mongod executable with the --install switch to install a service which will keep your mongo always up and running.
c:\>c:\mongo\mongod --install {arguments}
(Note: Version 1.4.3 has a bug which requires that the --install switch be run with the full path specified as shown above, however, I believe the nightlies include a patch for this.)

When using the --install switch, all other arguments become part of the execution call in the service. You can verify this by looking at the path in Windows service manager. For running on a server I like to include the --logpath and --quiet switches. The former so I can check for error outputs, and the latter to prevent my logs from growing to fast. Mongo 1.4 supports log rotation, but I haven't messed with it yet.
d:\>d:\mongo\mongod --install --dbpath d:\data --logpath d:\data\mongodb.log --logappend --quiet


Unknown said...

MongoDB-CSharp just released .90 Beta that includes full support for typed-collections as well as a fully operational linq provider. Check it out.

JP said...

Cool! Will do.

Back when I first tried it out it didn't have any of those features. Competition is good! :D