Introduction to MongoDB

NoSQL Database

5.00 ( 6 votes) | 2602

Hello everyone. Over the past few weeks i have been playing with MongoDB.  I would like to share with all of you what i learn so far. So let`s get started.

What is MongoDB?

MongoDB is a cross-platform, open-source, document oriented database also called as NoSQL database. You can find more information about NoSQL here. Data in MongoDB is a document, which is a data structure composed of field and value pairs. These documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents. For example

         name: 'MongoDB',
         databaseType: 'nosql',
         keywords: ['nosql','document-oriented']

MongoDB stores the document on the disk in BSON format. BSON is binary representation of JSON documents. You can find more information about BSON here.

Installation of MongoDB:

According to your operating system, you can download the appropriate version of MongoDB from here. I have windows 8.1 installed on my machine, so i downloaded the msi installer file. At the time of writing this article current production release version is 3.0.2. MongoDB uses odd-numbered version for development release that means if second number in version is odd then its development release otherwise a production release. For example second number for current production release 3.0.2 is zero which is an even number. At the time of writing this article current development release version is 3.1.1.  which has 1 as second number which is an odd number. After installation directory structure looks like (F: 1):

Directory structure after installing mongodb (F:1)

Main two components in package are "mongod.exe" and "mongo.exe". mongod.exe is the main database process and mongo.exe is interactive javascript shell interface used to interact with database. MongoDB requires a data directory to store the data. Default directory path is /data/db or we can specify the data directory path using --dbpath parameter. I have created a folder called data to store the data files (F: 2). We can name the folder as we like.

Data directory to store the data files (F:2)

Now we are all set to connect to MongoDB database. To connect to database open the command prompt and type the following command.

mongod.exe --dbpath "C:\ProgramFiles\MongoDB\Server\3.0\data"

Connect to MongoDB (F:3)

As you can see in image (F: 3) the server process has started and waiting for connection on port number 27017. This is the default port for MongoDB. We can change this port number by specifying –port option while connecting to the server. We can also install MongoDB as a windows service. To install MongoDB as a windows service execute the following command in command prompt.

mongod --dbpath "C:\Program Files\MongoDB\Server\3.0\data" --logpath "C:\Program Files\MongoDB\Server\3.0\logs\log.txt" --logappend --install

Parameters Description:

  • –dbpath parameter is used to specify the custom data directory.
  • -logpath parameter is used to specify path for a log file to store the standard output.
  • -logappend parameter is used to specify that append the new entries to the end of the file instead of overwriting the content of log file when mongod instance restart.

After executing the command in command prompt let`s open the log file. Content of the log file is (F: 4):

Content of Log file after executing the command (F:4)

As you can see in image (F: 4) that service has been installed successfully and we can start the service using command “net start MongoDB“. So let’s execute this command in command prompt.

MongoDB Service has been started successfully (F:5)

Great! We have now successfully installed MongoDB as a windows service (F:5). We can cross check that the service has been started in Windows Local Services (F: 6).

MongoDB service has started (F:6)

As the server process has started, we can now connect to the database using mongo shell (mongo.exe).  So let’s do it. We can start the mongo shell by typing mongo in command prompt. When we hit enter we will be prompted with blank window and a blinking cursor as shown in image below (F: 7).

Mongo Shell (F:7)

MongoDB shell version in image (F: 7) specifies that we are using 3.0.1 version of MongoDB and connecting to specifies that we are connected to test database.  In my next post we will discuss about the CRUD (Create Read Update Delete) operation in MongoDB.

Manoj Kulkarni - ManojKulkarni

Manoj Kulkarni

I am programmer, passionate blogger and foodie. I live in Nashik. I am a .Net developer. I like to learn new technologies to keep my self-updated and share that knowledge with my blog readers, friends and colleague.


Gaurav Raut - ManojKulkarni

Gaurav RautThursday, April 16, 2015 3:55 AM

Thankx dude it helped me a lot

Add a new comment