Introduction to MongoDB

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

MongoDB

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

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

Depending on 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 the current production release version is 3.0.2. MongoDB uses odd-numbered versions for development releases. That means if the second number in the version is odd then it's a development release otherwise a production release. For example, the second number for the current production release 3.0.2 is zero and that is an even number. At the time of writing this article the current development release version is 3.1.1 that has 1 as the second number and that is an odd number.

After installation, the directory structure looks as in (F:1).



Directory structure after installing mongodb (F:1)

The main two components in the package are “mongod.exe” and “mongo.exe”. mongod.exe is the main database process and mongo.exe is the interactive JavaScript shell interface used to interact with the database. MongoDB requires a data directory to store the data. The default directory path is /data/db or we can specify the data directory path using the –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 the MongoDB database. To connect to the database open the command prompt and type the following command.

  1. mongod.exe --dbpath “C:\Program Files\MongoDB\Server\3.0\data"“    



Connect to MongoDB (F:3)

As you can see in the image (F:3) the server process has started and is waiting for the connection on port number 27017. This the default port for MongoDB. We can change this port number by specifying the –port option when 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 the command prompt. 

  1. 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 the path for a log file to store the standard output.
  • logappend parameter specifies to append the new entries to the end of the file instead of overwriting the contents of the log file when the mongod instance is restarted.

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



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

As you can see in the image (F:4) that the service has been installed successfully and we can start the service using the command “net start MongoDB“. So let's execute this command in a 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 the mongo shell (mongo.exe). So let's do it. We can start the mongo shell by typing mongo in a command prompt. When we hit Enter we will be prompted with a blank window and a blinking cursor as shown in the image below (F:7).



Mongo Shell (F:7)

MongoDB shell version in image (F:7) specifies that we are using the 3.0.1 version of MongoDB and connecting to it specifies that we are connected to a test database. In my next article i will explain the create, read, update and delete (CRUD) operations in MongoDB.


Similar Articles