Installing MongoDB for MVC applications

Recently we started working on a big data project. Since lot of data is unstructured and structure of objects change as the need changed and grows with agile approach the group is following. After lot of discussions the team decided to go with NoSql database approach. We decided to use MongoDb. Since it was new for some team members, there was lot of learning experience. I decided to share this experience for anybody is looking into using MongoDb for their MVC projects.

In this first post, I will discuss the very first step that you will be taking. That step is installation of MongoDB in your environment. For our development, deployment and operations we are using Windows server. So in this post, I am going to focus on installation of MongoDb in Windows. For development enviroment, I will talking about insallation on Windows 7 and Windows 8.1 environment. MongoDb team has wonderful job in explaining how to go about installin database. You can read about it on their Install MongoDB on Windows page on their web site. I will just highlight quick steps and some issues that you may run into.

Installating MongoDB

  • Download MongoDB from this location. There are few points I would like to hightlight here.
    • Pick appropriate version depending on if you are installing on 32 bit or 64 bit version of operation system.
    • 32 bit version can only handle upto 2GB data. I would presonally recommend installing on 64 bit operating system to utilize larger data capacity and faster opeations.
  • MongoDb does not have a traditional windows installer that guides you through installations using wizard. So there are few manual steps that you will need to perform installation.
    • Unzip downloaded installation files into some known folder location so that it is easier to locate later on.
    • Like any other database installation, you can have executables for MongoDb in one location and data files at some other location. I will explain this later in this post. In our prototype development, I have installed MongoDb in C:\Program Files\mongodb 2.4.8\bin folder. I just chose standard Program Files to take advantage of the fact that windows operating system protects this folder from any non administrative actions. This adds additional security to installation of MongoDb.
    • MongoDB is a self contained executables. It does not require any DLL registrations or registry settings to run. So there is no other step that you need to perform at this point. Your installation of MongoDB is complete.

Test installation of MongoDB

If you will look at folder where you have unzipped MongoDB files, you will find few executables. Mongod.exe executables hosts execution engine for MongoDB. Start command line window and type Mongod.exe. Oops, you got the following error.

'MongoD' is not recognized as an internal or external command, operable program or batch file.

This was going to be expected. Since there is no wizard that helped in instalation, this mean we will have to update PATH field in our operating system environment so that our command line knows about MongoDB executables as well. Once you will add that folder location to your path, excute Mongod.exe from command line. You will see information like following.

        mongod --help for help and startup options
Fri Dec 27 09:02:17.057 [initandlisten] MongoDB starting : pid=7504 port=27017 
  dbpath=\data\db\ 64-bit host=Cheetah
Fri Dec 27 09:02:17.058 [initandlisten] db version v2.4.8
Fri Dec 27 09:02:17.058 [initandlisten] git version: a350fc38922fbda2cec8d5dd842
        Fri Dec 27 09:02:17.060 [initandlisten] exception in initAndListen: 10296
 ERROR: dbpath (\data\db\) does not exist.
 Create this directory or give existing directory in --dbpath.
, terminating
Fri Dec 27 09:02:17.061 dbexit:
Fri Dec 27 09:02:17.061 [initandlisten] shutdown: going to close listening sockets...
Fri Dec 27 09:02:17.062 [initandlisten] shutdown: going to flush diaglog...
Fri Dec 27 09:02:17.062 [initandlisten] shutdown: going to close sockets...
Fri Dec 27 09:02:17.063 [initandlisten] shutdown: waiting for fs preallocator...
Fri Dec 27 09:02:17.064 [initandlisten] shutdown: lock for final commit...
Fri Dec 27 09:02:17.064 [initandlisten] shutdown: final commit...
Fri Dec 27 09:02:17.065 [initandlisten] shutdown: closing all files...
Fri Dec 27 09:02:17.065 [initandlisten] closeAllFiles() finished
Fri Dec 27 09:02:17.066 dbexit: really exiting now

As you can see database has not started and we have error. The error is very self explanatory and tells us that we have not specified location where data is going to be stored. And error has instruction as well. For testing purpose I am going to specify a test location on command line.

C:\Users\Naveen>mongod --dbpath C:\Development\MongoDatabase\TestData --port 20029
Fri Dec 27 09:10:16.177 [initandlisten] MongoDB starting : pid=5836 port=20029 
   dbpath=C:\Development\MongoDatabase\TestData 64-bit host=Cheetah
Fri Dec 27 09:10:16.178 [initandlisten] db version v2.4.8
Fri Dec 27 09:10:16.178 [initandlisten] git version: a350fc38922fbda2cec8d5dd842
        Fri Dec 27 09:10:16.179 [initandlisten] options: { dbpath: "C:\Development\Mongo
Database\TestData", port: 20029 }
Fri Dec 27 09:10:16.252 [initandlisten] journal dir=C:\Development\MongoDatabase
Fri Dec 27 09:10:16.253 [initandlisten] recover : no journal files present, no r
ecovery needed
Fri Dec 27 09:10:16.334 [initandlisten] waiting for connections on port 20029
Fri Dec 27 09:10:16.334 [websvr] admin web console waiting for connections on po
rt 21029

Now we have no errors and database is up and running. You can see that I have specified parameters on command line. You must be wondering why did I specify port number. MongoDB has a default port that it uses to listen for socket commands. Since I alredy have a database running on my machine, so default port number was being used by that installation. Therefore I specified a different port number for illustration for this post.

You can see how easy it was to get MongoDB up and running. The following screens hot shows the files that MongoDB has created when it started up.

mongodb data folder

Execute query in MongoDB

Now that you have MongoDB running, you will want to check if it really works and can accept your database queries. Start a second command line window and type mongo.exe. This is shell that can be used to perform database operations. Following screen shot illustrates that I am able to connect to MongoDB test database. Now type db command on command prompt. You should get output value of test. This command simple lists databases that are configured in your system.

mongo shell

This completes your installation of MongoDB and doing quick test to make sure. In next post I will discuss more installation and configuration options.

comments powered by Disqus




21.0 °C / 69.8 °F

weather conditions Clouds

Monthly Posts

Blog Tags