First take on MongoDB

Recently, I have looked around for a good NoSQL database for my new project. I have been using MSSQL for a while but there seems to be more and more complexity as the data models are no longer table-compatible with sub-items and array and a volatile schema.

I have considered DocumentDB on Microsoft Azure on its very interesting indexing capability and the fully managed environment of Azure. However the current price is too expensive for me: 20 USD/month/collection. Some people suggested to put every type into a collection and use a field to distinguish them, but I feel that it is quite messy, especially for NoSQL starter like me. So I went back to some other popular choices such as RavenDB and MongoDB. RavenDB seemed to be a great fit at first as it seems to have very good interface for .NET (C# is my language of choice at the moment) and an awesome web interface, but I then realized that the free version actually has a lot of limit, and I did not plan to incur too much cost on this project.

So I decided to go with MongoDB, which is hugely popular NoSQL database, and seemed to be totally free for my need.

MongoDB supports Windows, Linux and Mac, so I spun up a Windows VM on Azure to try it out. Installation is extremely simple with the MSI package from the download page: https://www.mongodb.org/downloads.

I then followed the installation instruction in the Docs page (http://docs.mongodb.org/manual/installation), which shows how to start MongoDB server as a process or an Windows service. That webpage helps me to get MongoDB up quite fast, but the format (i.e. INI) of the mongod.cfg in that page is actually obsolete. You should you the new YML format so that you can use all the new configuration showed in this page (http://docs.mongodb.org/manual/reference/configuration-options), unless you are trying to use an old version (<= 2.4 I think).

I knew that I would have to set up an endpoint on the Azure Portal to open a port to the VM, and it took me a while to find the default port of MongoDB, which is 27017.

After that, I was a bit lost. The Docs page of MongoDB has quite a lot of information to starters like me. I did not even know how to check if the server was actually working correctly (I wished there were a nice web interface like in RavenDB) until I found some more instruction using the MongoDB shell (which is the mongo.exe file inside your installation folder): http://docs.mongodb.org/getting-started/shell/client.

I then tried to connect to database on my Azure VM from my computer using the MongoDB shell, and could easily perform some CRUD operations. It felt great! You can find event more shell functions here: http://docs.mongodb.org/manual/reference/method.

But of course I did not plan to rely on the shell client to communicate with the database. I wanted my ASP.NET 5 web site to be able to connect, so I went to C# Getting Started page (http://docs.mongodb.org/getting-started/csharp) to try out the C# driver. This page was very useful and help me setting up the connection and perform operation in no time. Just a pity that the nuget package for Core CLR was not available at the moment, so I had to disable Core CLR on my web application. After going through this page, there was actually a much better page for details information on the driver: http://mongodb.github.io/mongo-csharp-driver/2.0. You will be able to find a lot of useful information in there if you are planning to develop with the C# driver. I suggest going through the Reference section to understand how to use all the capability of the driver and MongoDB.

Then it got a bit scary when I realized there where no username and password or any authentication before accessing or even writing in the database, even after I had tried to add some user with read or write role to the database. It turned out that there is no client authentication enabled by default. You can read this page to understand more: http://docs.mongodb.org/manual/core/authentication, and follow this page to setting up client authentication: http://docs.mongodb.org/manual/tutorial/enable-authentication. I did not use the pem file and simply set security.authorization to enabled in the configuration. After that, you can connect to the database using http://username:password@hostname/databasename or using the MongoCredential class in the C# driver.

Until now I am very happy with MongoDB and the capability of the C# driver. All my data types can be serialized and deserialized perfectly (including enum, Guid, DateTimeOffset, TimeSpan and arrays). The driver also supports the familiar Linq, so I have no trouble writing queries. I will dig deeper and learn more about indexing and sharding capability. I believe it will be fun!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.