I had been hearing all of this talk of the NoSql movement, and, being the ever-curious tech type, I had to dig in and see what it was all about.
Of all the various options out there, MongoDB has been getting a lot of press lately. MongoDB is hailed as one of the top NoSql options out there for being ultra-scalable, and highly performant. You can see the long list of known production deployments out there.
With that context being set, I had to see what all the excitement was about. And, here’s how you can too:
1.) First, check out the Quickstart to get everything installed and setup. One note here: if you’re like me and you want to setup MongoDB to run as a service, check out the help in “mongod –h” but you can basically run “mongod –-install” and it will install it as a service for you.
2.) Next, go download the MongoDB .NET drivers from:http://github.com/samus/mongodb-csharp/tree/master
3.) Unzip, copy where you need to and add it as a reference to your project. You’re all set!
Your next step is to figure out how to work with MongoDB, and, as with any database (relational or not) you still have the basic operations: querying, updating, inserting and deleting.
So, let’s get started!
Connecting to the Server
So, how do we connect? Very simple. Check it out:
This assumes the default host (localhost), and port (27017; unless you configured it differently when setting up mongod). If so, there are constructor overloads to pass in a different host name and port.
Getting the Database
Don’t worry, you’re not in a completely foreign world here. There’s still the concept of a database. Here’s how you get the database instance (carrying forward the earlier code):
Again, simple, right?
Referencing the “Table”
Here’s where it gets interesting. There’s no concept of a table. The idiom to remember here is that MongoDB treats ever record as a “document”. So, instead of a table, there’s a collection of documents. Here’s the fun thing:
Because MongoDB is “schema-free” those documents structure can vary, even when in the same collection.
Yes, hard to twist your brain around, but this is really just a logical grouping of data. In most cases, however, people do put like structured documents into the same collections.
Let’s get our first collection:
In this case, we’re getting a collection called “Contacts”. Even if you’ve never saved anything into that collection, yet, it will still return you a valid collection to work with. So, this is how you create new collections.
Now that we have a collection, we can actually do stuff with it! Exciting, huh?
Introducing the Document: end all, be all.
In the MongoDB world, the Document class is the center of it’s universe. Because the very concept of the database is built upon the concept of this document, this is what you’ll find yourself using most. You use them to insert data, update data, delete data, query data. You will learn to live it, love it and adore the Document.
Here’s where I’m going to pick up the pace a bit because this is really where I got that “Ah ha!” moment, and I think you will to.
First, let’s insert some data:
Here, I did all the stuff from earlier (connecting, grabbing the database, getting the collection), and then I new’ed up a Document object. The Document object is nothing but a big Dictionary object, in this case.
So, I go to town. As you can see, I create a logical entity for a “Contact” in my mind, adding first name, last name, address, city, state and zip code, and I assign values respectively during the process.
Finally, I call the Insert method on my collection, passing in my document.
You have just created your first record in MongoDB. Stop, pat yourself on the back, think to yourself “Really, is that it?” and let’s move on. 🙂
Updating data (and really, pretty much everything else) is just as easy:
Easy! The only difference here (and what I didn’t show) is that I added the “_id” property of my document, after having inserted it earlier. This gives MongoDB reference on which document to update.
Now, deleting is just as easy. In our previous method, we just change the “Update” method to “Delete”. I could also leave the other properties, but in this case I already know the ID, so let’s just strip it down to that:
Again, easy enough!
Now, querying the database. This should look very familiar, and make a lot of sense if you’ve been keeping up:
Here, I create a Document object, I set the “City” property to “Dallas”, and I call Find(), passing in that Document object. The Document object, in this context, acts as the query to MongoDB to tell it what to search for.
From there, I iterate through Find().Documents, which is just a collection of Documents and print out the “FirstName” from each record.
Whew, that was long, sorry. And, there’s still a ton more to cover, but I’ll leave that for another date. Interesting, eh? It’s been enough to make me think about using it in a project myself. What about you?