Use ASP.NET MVC to Connect to MongoDB

Share on..
Tweet about this on TwitterShare on FacebookShare on Google+Share on TumblrEmail this to someone

I have recently ran into huge pain points when using MVC for doing things server side when I could be doing them client side and sometimes that also translates into headaches with SQL Server on the back end side because the technologies I’m using on the front end talk a different language as well as some tasks inherently are more well suited for a document database than a SQL Server or MySQL or insert database name here. Some people say that they love document databases like Mongodb because they are easy to set up and get running and are free, but if that’s the only reason you are using it, you must not be doing anything cool.

I assume you know something about MongoDB and have used it with another technology before or taken some type of course on it and understand the very basics. I also assume that you have taken a MVC tutorial or created at the least a simple website or that you understand C# and have used ASP.NET technologies in some capacity. If not you may have some issues understanding what the code you are pasting in actually does.

I needed to have a tutorial I could either revisit myself or point a colleague to that is new to MongoDB that get’s them up an running in MVC and that is the only thing this tutorial will do.

1) Create a new project: “ASP.NET Web Application” name it and choose MVC application.
2) Immediately open the package manager console and type:
PM> Install-Package mongocsharpdriver

This will add the MongoDB.Bson & MongoDB.Driver to References

3) Open the Home Controller, this next piece of code could benefit from using a setting to set up the variables for the mongo connection, but I am going to hard code them and let you worry about that, because this way I know you will get it work as the settings route could get you stuck if your are new to Visual Studio.
4) Add a Constructor with an instance of MongoClient inside, this will also need a connection string and I am breaking the components up into variables so that you can do with them as you wish.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//Make sure you add this
using MongoDB.Driver;
using FancyProject.Properties;

namespace FancyProject.Controllers
    public class HomeController : Controller
        //Controller Field for the database instance, allows.. 
        //us to access the database from our controller actions.
        public MongoDatabase Database;

        public HomeController()
            var port = 27017;
            var theConnectionString = "mongodb://localhost:"+port;
            var dbName = "someDbName";

            var client = new MongoClient(theConnectionString);
            var server = client.GetServer();
            Database = server.GetDatabase(dbName);
        public ActionResult Index()
            //The driver won't connect without a first command so..
            //I will trigger that by calling GetStats. Only required
            //for purposes of this demo.
            //Get access to server from database Controller Field
            //and then access the build information property & 
            //specify AllowGet to test in the browser
            return Json(Database.Server.BuildInfo, 

5) We have changed the Index action to return a json result of the server’s build information.

The idea for this document uses a similar approach to the way I connected to a MongoDB database from an application I built in a Pluralsight course. If you want to know more about working with MongoDB & ASP.NET visit Wes McClure’s course aptly titled: Using MongoDB with ASP.NET MVC which goes further in depth into using MongoDB in a ASP.NET MVC application and uses GridFS for storing images in a Real Estate application. The course is great for intermediate ASP.NET MVC developers.

Leave a Reply

You must be logged in to post a comment.