SyntaxHighlighter setup

Friday, February 10, 2012

MongoDB and the .NET Developer, Part 1: Mongo what?

I have found the whole NOSQL movement an interesting one.  And the product that I am most intrigued about is MongoDB.  I just went to their MongoDB Boulder event this week and was struck by how big a deal Mongo is in the PHP/Python/Node/etc... space but hasn't gotten much traction with .NET folk, so I am going to write a bit on getting started with it from a Visual Studio user's perspective.

First what is NOSQL and particularly MongoDB?  In general NOSQL is a growing set of technologies that are alternatives to the Relational Database.  Why bother?  We have so grown up with using the relational database that we assume it is the answer to every storage problem except few like file storage.  NOSQL questions that.  I will point out different benefits as I go, but for a more in depth treatment ask Wiki or the Duck

MongoDB is a fairly mature, open source NOSQL database produced by the company 10gen. There are binary downloads for Windows, OSX, many flavors of Linux and others.  It is easy to start it as a regular exe, but can also be run as a service. It manages data as collections of JSON/BSON documents.

One of the coolest things about it is that you can store rich hierarchical data in a very simple manner, in .NET basically handing an object off to your repository, it is serialized and stored and you are done.  Big deal you might say...we can do that pretty easily in a SQL table. First I promise it is much easier to do in Mongo than in SQL Server. But the real difference here is that all of that hierarchical data can be indexed by Mongo to make searches very efficient. For example, here is an simple example document of a person contact info:
{
  _id: 3409d3008c8d35c816390000
  firstName: “Matthew”
  lastName: “Nichols”,
  addresses: [
    {streetAddress: “123 Smith St, Unit Q”, city: “Denver”, state: “CO”, postalCode: “80206”, type: “Home”},
    {streetAddress: “123 Barney St”, city: “Denver”, state: “CO”, postalCode: “80211”, type: “Office”}
  ]
}

A collection in MongoDB could have many thousands of documents like these.  With the right indexes set up on the addresses array you could search for all of the people that lived in Colorado.

Features
So why bother?  There are a number of features that Mongo has that I find compelling:
  • A simpler and more flexible programing model: In Mongo your documents are just the data that your application hands it.  You are essentially just storing your domain model.
  • Replication is easy to setup. This allows for very high availability and data safety.  I will walk through setting this up in a later post.
  • Scales horizontally:  Database sharding is made pretty easy in Mongo. This allows the system to scale across multiple cheap machines rather than scaling vertically on increasingly expensive hardware.
  • Unlike many cross-platform open source projects Windows is well supported.  There is excellent documentation and a very competent driver.
Next post I will walk through setting up Mongo and some related tools on a development machine.

No comments:

Post a Comment