Using Mongo DB With ASP.Net MVC

Introduction

In this article series we will analyse how to use Mongo DB in an ASP.NET MVC application. Mongo DB is from a family of emerging NoSQL databases. It is a database for information no longer stored in rows but rather in rich documents. Mongo db uses BSON that is very similar to JSON.

Using Mongo DB in MVC

The following explains why to use Mongo DB in MVC:

  • Faster Development

    Unlike relational systems Mongo doesn't require you to expressly create databases, tables and columns.. It is all handled automatically by the very nature of flexible schemas. This cuts out a significant part of development.

  • Minimal Migrations

    Mongo minimises data migrations. The majority of data migrations are simply to add tables or drop things. This is all automatic with Mongo.

  • Easier Collaboration

    Fewer data migrations means much less pain sharing and merging database changes with other developers.

  • No Object relation impedance mismatch

    Instead of having separate tables for payment and order items, everything is messed up within an older document itself. There is no need to worry about unnecessary foreign keys and how to maintain the integrity of false relationships. However when we represent our model in code the resulting documents in Mongo will fit our model. This removes the object relational impedance mismatch.

    No Object relation impedance
  • Maintainable design
  • Rich Behaviour
  • Scalability
  • Grid FS

Mongo has Grid FS for storing querying and associating files with data in our applications.

We will learn the following things in this article series:

  • Organization of .Net Driver
  • Connecting to Mongo server
  • Fundamentals of serializing objects
  • Storing and modifying through MVC applications
  • Modelling
  • Querying
  • Grid FS and Files
  • Avoiding difficulties

Documents in Mongo DB

Documents are a set of name/value pairs, also known as key value pairs. In the driver these are refered to as elements. In the preceding document customer id 1234 is an example of an element with the name of customer id and a value of 1234.and the payment is an embedded document and also a value can be an array.

Let's experiment with the BSON document in the following Test Project.

  • Step 1: Create a new project. Open Visual Studio 2013 then click on "File" -> "New" -> "Project..." then select "Windows" -> "Class Library" then name it "Mongo Tests".

    create a new project in asp.net

  • Step 2: Open a Package Manager Console and install the Mongo C# driver and Test Framework N unit.

    package manager console

MongoTests class

Create a class named MongoTests as in the following:

  1. using System;  
  2. using MongoDB.Bson;  
  3. using NUnit.Framework;  
  4.   
  5. namespace MongoTests  
  6. {  
  7.     public class BsonDocumentTests  
  8.     {  
  9.         [Test]  
  10.         public void SampleEmptyDocument()  
  11.         {  
  12.             var document = new BsonDocument();  
  13.             Console.WriteLine(document);  
  14.         }  
  15.     }  
  16. }  
 Create a class named

Now let's try to add some elements.

One way to add elements is through the add method. BSON also supports adding elements through C# collection initializes:

  1. [Test]  
  2.     public void AddElements()  
  3.     {  
  4.         var people = new BsonDocument {{"Name"new BsonString("sagar")}};  
  5.         Console.WriteLine(people);  
  6.     }  
 add some elements

Summary

In this article we have learned about Mongo db. Mongo Db is named as the best database management system of 2013 by db engine and is increasingly being added to job postings and desired skill sets. In my future articles we will see more on Mongo DB.