MongoDb: Install as Service and use .NET Drivers

How to get up and running in five minutes with MongoDb as a windows service

"Wrong is Right" -- Thelonius Monk


Of all the object or Document databases available, I think MongoDb holds the most promise. This kind of database is not new, I remember experimenting with Db40 at least three years ago. But as far as ease of use is concerned, I believe, after some experimentation with the 2 main "drivers", that MongoDb as a Windows Service, combined with the NoRM C# database driver ("provider") is the best combination.

Who is using MongoDb, and what are they using it for? Well, why not take a look at the list? You’ll see some very familiar names:

http://www.mongodb.org/display/DOCS/Production+Deployments

MongoDb is also very fast. How fast? Michael Kennedy did some benchmarks with MongoDb compared to SQL Server 2008 here:

http://www.michaelckennedy.net/blog/2010/04/29/MongoDBVsSQLServer2008PerformanceShowdown.aspx

And while his choice of LINQ To SQL for the SQL Server comparison is probably not a good one, the differences in insert and update speeds are still quite dramatic.

First, let’s get up and running with MongoDb. There are a few “Gotchas” involved. By following the simple instructions I’ll provide next, you can be up and running in less than five minutes with no headaches at all!

1. First download the pre-built binaries, either 32-bit or 64-bit. I recommend 64-bit if you have an x64 machine and OS.
http://www.mongodb.org/display/DOCS/Quickstart+Windows

2. Create a folder at the root of your C:\ drive and name it “MongoDb”. Unzip the distribution into that folder.

3. Create the default repository folders: Again, at the root of your C:\ drive, create a new folder “data” and then under that folder, another folder called “db”.

4. Install the service. Most tutorials tell you to run the mongod.exe daemon from the command line. That’s bunk if you expect to put something into production. Execute mongod.exe –install from a command prompt in the C:\MongoDb\bin folder. This will install mongod.exe as a Windows Service – but you are not done!

5. Due to “developer error” when building the most recent distribution that I used, the registry entry for the service image path is incorrect. Open up REGEDIT and navigate to:

HKLM\System\Services\MongoDb\ImagePath You will notice that the value there is:

"C:\mongodb\bin\mongod" –install

That’s wrong – what it will do is when the Service Control Manager attempts to start the service, it’s going to try to install itself again instead of starting. Change this value to:

"C:\mongodb\bin\mongod" –service and close REGEDIT.

Of course there are many command line options but these are the defaults – including the data directory, so why not start out with something you know will work.

You are done. Start the service. There is nothing else to do now, except download and use your favorite driver. That’s Mongo-talk for “Provider” for us .NET folks.

The two best .NET drivers for MongoDb are the MongoDb-CSharp Driver:
http://github.com/samus/mongodb-csharp
And the NoRM driver:
http://github.com/atheken/NoRM

Of the two, both of which I have experimented with, NoRM is currently the best option, in my opinion, as it provides for strongly typed CRUD operations on your own POCO classes with a minimum of code, combined with some very powerful LINQ abilities for querying. And besides, Karl Seguin is working on it, so you know it's got to be good!

For example, the following code opens a MongoDb connection and upserts a Quotation class instance directly into the Mongo database:

using (var mongobongo = Norm.Mongo.Create(Helper.ConnectionString()))
{
var coll = mongobongo.GetCollection<Quotation>();
Quotation q = new Quotation()
{
Author = this.txtAuthor.Text,
AuthorInfo = this.txtAuthorInfo.Text,
Quote = this.txtQuotation.Text,
Subject = this.txtSubject.Text
};

coll.Save(q);

}

An upsert means that if the item is already present in the database, it gets updated – and if not, it gets inserted. (Don’t worry – I’m going to give you a starter Quotations database!)

You can create indexes on any field like this:

coll.CreateIndex(u => u.Quote, "quote", true,IndexOption.Descending);

Querying is just as easy:

using (var mongobongo = Norm.Mongo.Create(Helper.ConnectionString()))
{
var coll = mongobongo.GetCollection<Quotation>();
var results = coll.Find().Where(x => x.Quote.Contains(this.TextBox1.Text)).Take(10).ToList();
this.DataList1.DataSource = results;
DataList1.DataBind();
}

I would call that pretty compact code! Fun, even. Forget about NHibernate mappings and so forth – in fact, let’s forget about all this ORM business and go Back to the Future - where we can just stick our "Junk" directly in the Trunk!

You can view your server statistics in a web page with the following url: http://localhost:28017/   If you have --rest in your startup parameters (see registry entry above) then you can use the REST interface to see all kinds of things from this "web site".

MongoDb Command Line parameters are here. In particular the --bind_ip <ip> switch specifies a single IP address that the server will listen on.

The downloadable sample Visual Studio 2010 solution contains a web application that has two pages:

1) The Default.aspx page that lets you query the database based on a dropdown selection of Author, Subject, or Quote – and displays the resulting quotations in a DataList, or
2) The AddQuote.aspx page that lets you insert a new quotation into the MongoDb.


In order to install my Quotations MongoDb database, you need to temporarily stop your MongoDb Service and copy the two provided files, Quotes.ns and quotes.0 from the \db folder in my solution into your C:\data\db folder. Now restart the MongoDb service.

The rest of the app should work out of the box with no configuration. You can download the sample solution here.

I have also compiled a CHM Html Help file for the NoRM Driver here.

Enjoy!

By Peter Bromberg   Popularity  (5140 Views)