Using MongoDB in C#

One recent technology I had heard an awful lot about but had not had a chance to use was NoSQL Databases. The idea that I could store data in an object oriented format, and not a relational format excited me, as I could see it being very useful and easy to fit into applications, and render something like Entity Framework pointless, but there were several nagging questions as to how it would fit in and whether it would be capable of performing the role I wanted it to. So I eventually took my chance and developed a small application in it at my last work hack day, developing a simple holiday calendar in C#. It was an interesting project and I learnt a lot about MongoDB (the NoSQL database I chose to use), but more importantly, I learnt about integrating with a C# application, and this is what I would like to talk about here.

I didn’t find a huge amount of help when looking to use MongoDB in C#. The official documentation for the MongoDB C# driver is ok, but not overly helpful. However the whole process was very simple thanks to an amazingly easy install and setup process for MongoDB, and then some helpful nuget packages coming to the rescue for C#!

The MongoDB documentation for installing on Windows is great, providing a step by step guide to install and setup as a service. The whole process literally took me around 5 minutes, and then I had my MongoDB running on my machine. You may also wish to install an admin GUI as MongoDB does not come with one. I chose MongoVUE as it was simple to setup and use.

Once installed, setting up my application to connect to MongoDB was extremely simple. If you separate out your data layer (which you really should), adding MongoDB is extremely simple. There are two nuget packages that help, the first being the official MongoDB C# driver, and on top of that the MongoRepository nuget package provided with a ready made repository for connecting to my MongoDB database, with a Repository class prebuilt with CRUD operations.

MongoDB nuget packages

Now you can create your entities in code. A simple example would be the below “Employee” entity:

Entity is a class provided by MongoRepository. As long as all our Entities inherit this class we can then use the Repository class from MongoRepository for our entities like below:

However having our domain inherit from our Repository is not keeping our Domain loosely coupled so you would be best served writing your own Repository on top of MongoRepository to keep the separation between the domain and the repository. I didn’t have time in my hack day to do this, however MongoRepository does recommend this approach.

The last piece of the puzzle is adding the connection string to your app.config. MongoDB has very simple connection strings as you can see below:

The first part of the connection string tells you to use mongodb, the second (localhost) is the connection to the database machine, and the third is the database name (HolidayCalendar in this case).

Now by simply doing the following we can add to our database:

It’s as simple as that. No need to setup a table and structure for employees, no need to setup the database even. When you ask MongoDB for the HolidayCalendar database, if it doesn’t exist, it creates it. When you tell it to add an employee, again if it doesn’t have anywhere to save it, it creates a place. It is so simple. I will get into the various benefits and downsides of MongoDB in a late post, but I hope this highlights how easy it is to start to use in your C# applications!

 

One thought on “Using MongoDB in C#

  1. Was always willing to learn, try MongoDB. Your post is clear and concise. I am now inspired to try and read about MongoDB lot more! Thanks!

Leave a Reply