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

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

  • You will learn the basics of MongoDB administration.
  • You'll learn how the .Net driver is organized and how to use it to connect to the Mongo Server.
  • You'll learn the fundamentals of how .Net types are serialized and stored in Mongo and how to customize the process to fit your specific needs.
  • You'll learn how to create an update and remove data from your MVC Applications.
  • How to build models with rich behavior without the overhead of mapping  to our relational model.
  • You'll learn how to use both LINQ and Mongo's Query document style to query information from your application and how to combine them to build the most flexible of queries.
  • You'll learn how to use GridFS to store files and associate them with documents in your MVC applications.
  • You'll also learn how to avoid common pitfalls.

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?
  • Faster Development: unlike relational systems Mongo doesn't require you to explicitly create a database, tables and columns; it's all handled automatically by the very nature of flexible schemas. It significantly cuts the development effort.
  • Minimal Migrations: Mongo also minimizes data migrations.
  • Easier Collaborations: Fewer migrations mean much less pain sharing and merging changes with other developers.
  • Maintainable Design: With relational storage, building a rich model requires a lot of overhead to stitch together the results from many tables and then map that to separate models in your applications. This level of overhead either isn't justifiable or is just too difficult to maintain. This leads developers to constrain the code to fit the relational model resulting in highly procedural code, duplication, foreign key identifiers, cascading deletes and so on, scrambling all the business logic. All of this results in an application that is very difficult to maintain.
  • Rich Behavior: As you start to create rich data models, you will find yourself adding rich behavior to those models as well. For example when order items are contained in an Order model, it's easier to have the Order model calculate the total instead of doing the center Controller or otherwise.
  • Scalability: Mongo Architecture allows you to scale as your traffic increases.
  • GridFS: Mongo also has a fantastic system, GridFS, for storing querying and associating files with data in your application.

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.

 
Feel free to leave any comment or feedback!!
 


Similar Articles