MongoDB Tutorial : Introduction and Step by Step Installation

This article talks about different MongoDB concepts and also gives step by step Installation guideline on Windows OS.

MongoDB (from “humongous”) is an open source document-oriented NoSQL database system.

MongoDB is part of the NoSQL family of database systems. Instead of storing data in tables as is done in a “classical” relational database, MongoDB stores structured data as JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.

Installing and Running MongoDB

Installing MongoDB on Windows only requires you to unpack the archive and using the necessary files for running the database.
Steps to install MongoDB are as follows:
1. Download MongoDB: Download the MongoDB from Direct link for downloading the MongoDB Archive –
2. Unzip the downloaded binary package to the location of your choice. You may want to rename mongo-xxxxxxx to just “mongo” for convenience.

E.g. C:\mongodb-win32-x86_64-2.0.6

3. Check the contents of the Archive. The bin folder should contain 11 executable files.

4. Creating a Data Directory
By default MongoDB will store data in \data\db, but it won’t automatically create that folder, so we do so here: in windows shell.

C:\> mkdir \data

C:\> mkdir \data\db

Or you can do this from the Windows Explorer, of course.
If you prefer to place datafiles elsewhere, use the –dbpath command line parameter when starting mongod.exe.
E.g. Data Files location

D:\> mkdir RecommenderDataFiles\data

D:\> mkdir RecommenderDataFiles\data\db

5.  Running and Connection MongoDB Server
bin\mongod is the MongoDB Server Daemon. This listens to the MongoDB requests and also handles the data files of the DB. Run mongod –help to see other options.
To run the database, click mongod.exe in Explorer, or run it from a CMD window
MongoDB Path – D:\mongodb-win32-x86_64-2.0.6

D:\> cd mongodb-win32-x86_64-2.0.6\bin

D:\ mongodb-win32-x86_64-2.0.6\bin> mongod –-dbpath “D:\RecommenderDataFiles\data”

This will start the mongod.exe server running, and you will see log messages displayed in this window as the server runs.

Note: It is also possible to run the server as a Windows Service. We will do it in the next section.

6. Checking the Working on MongoDB by the Admin Shell
Now, start the administrative shell, either by double-clicking mongo.exe in Explorer, or from a new CMD prompt. By default mongo.exe connects to a mongod server running on localhost and uses the database named test. Run mongo –help to see other options.

D:\> cd mongodb-win32-x86_64-2.0.6\bin

D:\ mongodb-win32-x86_64-2.0.6\bin> mongo

> // the mongo shell is a javascript shell connected to the db

> // by default it connects to database ‘test’ at localhost

> 3+3 6 > use test

> // Using the test DB > db


> // the first write will create the db: (Lazy Writes)

> { field : 1 } )

> // inserting a document with field „field‟ and value 1. After this insert only, both test and foo are created

> { _id : …, a : 1 }

> // a “SELECT * FROM FOO” kind of statement

> show dbs

> // showing all the DBs in mongoDB …

> show collections

> // showing all the collections present in DB selected …

> help

Congratulations, you’ve just saved and retrieved your first document with MongoDB!
foo – A Collection in the test DB. It is equivalent to a table in RDBMS.

Installing MongoDB as a Windows Service

MongoDB has native support for installing and running as a Windows service. It is used for managing MongoDB with Windows Services framework.
The main command line options for working with MongoDB as a Windows Service are

–install :  Install as a Windows service, then exit without starting the service

–remove  : Remove the Windows service, stopping it first if required

–reinstall  : Remove and then reinstall the Windows service


D:\> cd mongodb-win32-x86_64-2.0.6\bin

D:\ mongodb-win32-x86_64-2.0.6\bin> mongod –-dbpath “D:\RecommenderDataFiles” –install …

Note : It requires other arguments for its function. Please don’t run the above command before you know the arguments necessary.

You may use the following options with –install, –reinstall and –remove: in the examples, {arg} is any text. Use quotes around the text if it includes spaces

–serviceName  {arg} : Specify the service name (used with “net start/stop”, and usually does not include spaces). E.g. net start “MongoDBSrvc1” – More than one instance can be installed as a Service by specifying unique Service Names and Service Display Names.

–serviceDisplayName {arg} : Specify the service’s display name

–serviceDescription {arg} : Specify the service’s description – This does not have to be unique, but a logical explanation of each service would be useful to understand would be

–serviceUser {arg} : Specify login account for running the service (account must have the “Log on as a service” right) – it is used with –install and –reinstall options to configure the login account used by the Service Control Manager. – If no serviceUser is specified, the system account LocalService will be used.

–servicePassword {arg} : Specify password for login account – it is used with –install and –reinstall options to configure the login account used by the Service Control Manager.

–service : Indicates that the process is running as a service: do not use on the Windows command line

Because the running service does not have a command window to display its log output, the –logpath option must be used when installing the service. In most cases you will also want to specify –logappend so that restarts of the service append to the existing log file.

–logpath {arg} : Log file to send write instead of stdout – has to be a file, not a directory

–logappend {arg} : Append to logpath instead of over-writing

Other options that you include on the command line with –install will be added to the command line that is used to run the service. For example, if you execute:

mongod –logpath d:\mongo\logs\logfilename.log –logappend –dbpath d:\mongo\data –install

This will cause a service to be created with service name “MongoDB” and display name “Mongo DB” that will execute the following command line:

mongod –logpath d:\mongo\logs\logfilename.log –logappend –dbpath d:\mongo\data –service

If any file specification includes spaces, put quotes around the file specification.

mongod –logpath “d:\my mongo\logs\my log file name.log” –logappend –dbpath “d:\my mongo\data” –install

Thus, you can successfully install MongoDB as a service. While choosing the –install option, would need to specify the –logpath and –logappend option for the log redirection.

D:\> cd mongodb-win32-x86_64-2.0.6\bin

D:\ mongodb-win32-x86_64-2.0.6\bin> mongod –-dbpath “D:\RecommenderDataFiles” –logpath “d:\MongoLogs\MongoDBLogFile.log” –logappend –install

D:\ mongodb-win32-x86_64-2.0.6\bin> net start “MongoDB”

Keep Coding…

Varad Meru
Author of this article : Varad Meru – A power packed Hadoop Developer also comes a with a strong experience on Mahout and Recommender systems

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s