MVC Application With MongoDB - Part 1

Step 1: Let’s start with MongoDB server installation for our application. In this application I am using MongoDB 3.0.4 server. The following image indicate the installation of server. Setup files downloaded from MongoDB official website.


Step 2: Now it’s time to check server has installed properly or not on machine. So go to location ‘C:\Program Files\MongoDB\Server\3.0\bin’. After successful installation of server bin folder contains the following mentioned files.


Step 3: Start MongoDB.

To start MongoDB, execute the following from the command prompt: C:\ProgramFiles\MongoDB\Server\3.0\bin\mongod

This will start the main MongoDB database process. Now waiting for connections message in the console. Output indicate that mongod.exe running successfully.


Step 4: Connect to MongoDB in order to perform database operations

Run command from new command prompt: C:\Program Files\MongoDB\Server\3.0\bin\mongo

Execute command ‘show dbs’ – it shows a list of database present at server.

Execute command

MVC Application with MongoDB

MVC Application UI: We are going to design the following user interface using MVC & all CRUD operations are performed using MongoDB database, so all the methods which we are going to use are with respect to MongoDB database syntax.

MVC Application

Application Folder & File Structure are as follows:

MVC Rgistration

UserRepositary Model Class File: We are going to use this model class to perform CRUD operation in MongoDB database.

Create Application Database

  1. using MongoDB.Bson;     
  2. using MongoDB.Driver;     
  3. using MongoDB.Driver.Builders;     
  4. using System;     
  5. using System.Collections.Generic;     
  6. using System.Linq;     
  7. using System.Web;     
  9. namespace MvcRegistration.Models     
  10. {     
  11.     public class UserRepositary : IUserRepositary     
  12.     {     
  13.         ObjectId id = new ObjectId();     
  15.         MongoClient client = null;     
  16.         MongoServer server = null;     
  17.         MongoDatabase database = null;     
  18.         MongoCollection UserDetailscollection = null;     
  20.         string connectionString = "mongodb://localhost";     
  21.         private List<UserModel> _UserList = new List<UserModel>();     
  23.         public UserRepositary()     
  24.         {     
  25.             try    
  26.             {     
  27.                 client = new MongoClient(connectionString);     
  29.                 server = client.GetServer();     
  31.                 database = server.GetDatabase("MVCDB");     
  33.                 UserDetailscollection = database.GetCollection<UserModel>("UserModel");     
  34.             }     
  35.             catch (Exception ex)     
  36.             {     
  37.                 Console.WriteLine(ex.Message);     
  38.             }     
  39.         }         
  40.     }     
  41. }  
Above code will create 'MVCDB' database at MongoDB server.

We can test weather database is created or not using command 'show dbs'.

show dbs