Working With MongoDB in MVC Application

Dheeraj Kumar Gunti

Reading Time : ( words)

NoSQL DB is the one more alternative for traditional db. There are different flavours in NoSQL DB like DynamoDB, mongoDb, CouchDB etc. In this article we will see how to work with Mongodb. You can look for an article in RavenDb here Working With RavenDB

MongoDB is a cross-platform document-oriented database. NoSQL DB's are used to build high performance applications effectively. MongoDb has dynamic schemas and the format is called as BSON.

First developed by the software company MongoDB Inc. in October 2007 as a component of a planned platform as a service product, the company shifted to an open source development model in 2009, with MongoDB offering commercial support and other services. MongoDB has been adopted as backend software by a number of major websites and services, including Craigslist, eBay, and Foursquare among others. MongoDb is one of the popular database management system and most popular for document stores.

Lets jump in working with MongoDb in an MVC application.

Initial step starts with installing and configuration. You can download Monogdb from Mongodb site in 32bit and 64bit flavors.

32 Bit :

64 Bit :

Once the download is done, Install the application and please do remeber the location where the mongodb is installed. Typically it installs in C:\Program Files\MongoDb. As we got to know MongoDb is a document database where all the documents are stored in a physical location in hard drive. MongoDb recommends to have a directory named "data\db" in C Drive where all the documents are stored. Mongo offers cloud Manager where you can use PAAS (Platform as a service) for all your data.

Go To Command Prompt and navigate to installed path of MongoDb as below.

cd C:\Program Files\MongoDB\Server\3.0\bin

and run the executable "mongod". This will run the mongodb server in Port 27017.

So, Initial step of setting up database is done.

Lets start with small application.

Create a MVC application and Dowload, Install the MongoDbCsharpDriver

Install-Package mongocsharpdriver -Version 2.1.1

This will install

  • MongoDB.Bson
  • MongoDB.Driver.Core
  • MongoDB.Driver

Create a class named "Employee" with the below properties.

In the above snippet we have used BsonIgnoreExtraElements, it is used to ignore elements when the class is deserialized.

Open HomeController and create an object for MongoClient

As we got to know like mongodb server runs on 27017 port, so we are specifying that port and as we are running in our local machine so we are targetting localhost here. If the server is running in different machine then we need to specify the server name and the port number.

Lets write pull the data from the database

Here, using MongoClient object we are using GetDatabase method which takes database name and some database settings (optional) as parameters. If the name provided here is not there in the database then this method will create the database for you.

Collection is a group of MongoDB documents. Collections do not enforce a schema. As every collection will have multiple documents with in it and A document is basically a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

We are going to fetch the data using GetCollection method from database.

Using the find method we are fetching all the employee information and returning it to the view.

Lets see how to insert data in to the database.

We have,

InsertOneAsync, DeleteOneAsync, ReplaceOneAsync, UpdateOneAsync, UpdateManyAsync methods which performs all the other CRUD operations.

Find the Sample here Download Sample

Enjoy Coding.....