Introduction and Goal

With this article I'm starting a step-by-step series on how to use MongoDB with ASP.NET MVC. In the course we will go from the basic to the advanced operations of MongoDB and gradually how to use it with ASP.NET MVC.

What you will learn

In this article “Day-1” of the series, we will learn how to get started with MongoDB. We will go through the introduction to MongoDB, why we should use MongoDB with ASP.NET and then download and instal it today. I chose to let you dive into the basics before moving towards the advanced lessons so that the series covers the novice developers as well or whosoever is not so familiar with MongoDB.

Introduction to MongoDB

MongoDB is from the family of emerging NoSQL or Not Only SQL databases. It's a document type database where information is no longer stored in rows but rather as rich documents.

MongoDB Documents

MongoDB uses BSON, very similar to JSON to store documents. BSON (or Binary JSON) is a binary encoding of JSON-like documents that MongoDB uses to store information in documents. It adds support for the Date and Binary (BinData) data type that isn't supported in JSON.

Why use MongoDB in MVC?

Last but not least, according to DB-Engine, the fifth most popular storage system and the number one NoSQL most popular solution (as of the date I'm writing this article, "2014-04-07").

Let's get started

In order to get started with MongoDB you need to first download and install it on your system. Visit MongoDB downloads URL: mongodb downloads and download the supported/desired version of MongoDB. As of now when I'm writing this article, the site looks like this:

MongoDB download

The good part here is, you don't need to install it at all. Just unzip the Zip file that you've downloaded and start exploring MongoDB, no installation at all. The contents inside the folder after you unzip it will look like this:

MongoDB Folder

You can see the two executable files “mongo” and “mongod” (notice the additional “d” appended to mongo) that I've pointed to in the picture above. They're the key of the entire play, at least “mongod.exe” if not both of them.

mongod.exe is solely responsible for starting the MongoDB server whereas mongo.exe plays the role of the database client, using that you can access the MongoDB databases, collections and what not.

Procedure to start Mongo Server:
  1. Start a command prompt (cmd)
  2. Navigate to the MongoDB folder that you've unzipped.
  3. Enter “mongod.exe --dbpath db” , and hit Enter.

Here, the - dbpath parameter specifies the database path. You can use any other name if you don't like “db” or you want some other name to be your database in there. If everything goes well then you'll see the console screen something like this:

console screen

MongoDb uses two default ports for storage management: 27017 and 28017 (refer to the picture above). It says its “waiting for the connections on port 27017” and, “admin web console waiting for connections on port 28017”. It means you can manage a MongoDB database in two ways, one is by using a web browser and another is using any other client application.

Let's give them a try. Fire up your browser, put http://localhost:28017 in the address bar and see what happens next. It will show you a dashboard kind of screen that contains information about the MongoDB Databases, their status and other info.

MongoDB Databases

You can also use the REST URLs present in the top section to get various kinds of information related to the MongoDB database, but ensure you start MongoDB with the “-rest” parameter. For example:

"mongod.exe --dbpath db -rest"

There are many other parameters available for use. You can always refer to the help manual in the console (cmd) using “mongod /?” or visit mongodb for more details.

Getting to the second executable file, it is the client that helps you talk to your MongoDB database and perform db operations.

To start with, start another console then navigate to the MongoDb folder then enter “mongo.exe” and hit Enter. It will start a MongoDB Client console application. The MongoDB console is often called Mongo Shell.

Mongo Shell

You must have noticed that I didn't specify any port but still it's connected. It uses the default server (localhost) and port 27017 to connect to the MongoDB server if you don't specify any server address and port. The default database it will try to connect to is "test".

Make sure the MongoDB server is running, otherwise the client will not be able to communicate and eventually will throw an error that it couldn't connect to the MongoDB server:

MongoDB server

What Day-2 contains?

In the next article we will learn how to perform basic CRUD operations in MongoDB and much more. The next day will get you started with reading and writing data to MongoDB.

Getting Started With MongoDB and ASP.Net MVC4: Day 2

Getting Started With MongoDB and ASP.Net MVC4: Day 3
Feel free to leave any comment or feedback!!