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.
- 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".
- Step 2: Open a Package Manager Console and install the Mongo C# driver and Test Framework N unit.
MongoTests class
Create a class named MongoTests as in the following:
- using System;
- using MongoDB.Bson;
- using NUnit.Framework;
-
- namespace MongoTests
- {
- public class BsonDocumentTests
- {
- [Test]
- public void SampleEmptyDocument()
- {
- var document = new BsonDocument();
- Console.WriteLine(document);
- }
- }
- }
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:
- [Test]
- public void AddElements()
- {
- var people = new BsonDocument {{"Name", new BsonString("sagar")}};
- Console.WriteLine(people);
- }
SummaryIn 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.