We all work with objects in our .NET application. But when it is time to persist those objects we are always inclined towards the relational database. In this article we are going to demonstrate how to use MongoDb database to store our objects.
Why Document Database?
We are in a constant battle to correctly convert our object graph to a relational model. Sometimes these conversions are simple but in most applications it becomes overly complicated. We also face similar challenges when retrieving the database from the relational model into the object model.
The document database allows to persist the complete object which can easily be retrieved without threading data together to form a valid object.
Downloading MongoDB:
Before downloading MongoDb we need to create few default folders. Go to the C:\ drive and create the "data" folder. Now, create "db" folder inside the "data" folder.
For Windows 32 bit download the following zip file:
Download
For Windows 64 bit download the following zip file:
Download
Next, run the following command to start the MongoDb server.
You can check your installation by running the Mongo shell. Run another command line and type the following command:
The great thing about MongoDb is the support for drivers. There are tons of different drivers available which will allow you to work with many supported languages. This article demonstrates how to work with MongoDb using the C# driver.
You can download the C# driver using the following link:
C# driver for MongoDb
Getting Started with MongoDb:
Start Visual Studio and create a simple console application. Now, add the reference to the following MongoDb driver assemblies.
MongoDB.Driver
MongoDB.GridFS
MongoDB.Linq
Once the references has been added you are ready to use MongoDb using C#.
NOTE: Make sure your MongoDb server is running. You can start the server by running the following command from command line:
The following code connects to the MongoDb server.
Let's perform something practical and insert a record in the database.
In the above code we connected to the MongoDb server using mongo.Connect(). The getDB command will create the database. If the database already exist then it is returned. The db.GetCollection command gets the "categories" collection. If the collection does not exists then it is created.
The category is of type Document. The properties are added to the document in the form of dictionary values. Finally, after adding all the properties the category Document instance is added to the "categories" collection.
If you run the above code it will insert a single item in the "categories" table of the "northwind" database.
Remember the "data\db" folder we created at the start of the article. Visit that folder again and you will see your new database file "northwind".
Even though you only inserted a single record the size of the file is "16MB". This is the default size of a new database file which contains the hash maps of all the namespaces in the db.
Retrieving the Persisted Items:
In the last section we inserted a new document to our document database "northwind". Let's see how easy is it to fetch the the persisted item.
If you remember correctly we never inserted any value for the "_id" field. The "_id" field is automatically added by MongoDb as a unique field.
If you run the above code you will get the following output:
You can even specify a criteria when fetching items from the collection. In the code below we are only fetching the document whose attribute "Name" is "Beverages".
Conclusion:
The code samples discussed in this article accessed the documents stored in MongoDb database using "Key". The "Key" is case sensitive which means "Name" is not equal to "name".
Although the use of document works but it is not practical in large applications since the margin of error for naming key is huge. In the next article we will demonstrate how to convert a CLR object to a Document and also store hierarchical objects.
[Download Sample]