What is MongoDB and How it works?

Jul 02, 2016




Objective: Main objective of this blog is to learn about MongoDB and working with MongoDB.


MongoDB is an open-source document database, and leading NoSQL database. MongoDB is written in C++. MongoDB is a Cross-platform , Document Oriented database that provides High-performance , High-availability and Easy Scalability. MongoDB works on concept of Collection and Document.

Advantages of mongoDB

  • MongoDB is a document database. In that one collection holds different documents. for example Number of fields, Content and size of the document etc….
  • No complex Join
  • MongoDB is east to scale
  • MongoDB  Support document base Query Language that’s powerful as SQL

Why should use MongoDB

  • Replication & High availability
  • Rich Queries
  • Fast In-place Update

Where should use MongoDB

  • Big-Data
  • User data management
  • Data Hub

STEP 1: Install MongoDB on windows / Ubuntu

1.1 How to install on Windows

To install the MongoDB on windows pc, first you need to download the latest release of MongoDB from http://www.mongodb.org/downloads .

Make sure you get correct version of MongoDB depending upon your windows version like Windows XP, Windows 7, Windows 8, Windows 10 with 32bit or 64bit.

To get your windows version open command prompt and run following command.

Now extract your downloaded file to c:\ drive or any other location.

Make sure name of the extracted folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-[version]. Here [version] is the version of MongoDB download.

Now open command prompt and run the following command

If you extract with different location then you need to reach that path using this command cd FOOLDER/DIR and then run above command

MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:\data\db. So you need to create this folder using the Command Prompt. Execute the following command sequence.

If you have install the MongoDB at different location, then you need to specify any alternate path for \data\db by setting the path dbpath in mongod.exe. For the same issue following commands.

In command prompt navigate to the bin directory present into the mongodb installation folder. Suppose my installation folder is E:\set up\mongodb

This will show waiting for connections message on the console output indicates that the mongod.exe process is running successfully.

Now to run the mongodb you need to open another command prompt and issue the following command.

This will show that mongodb is installed and run successfully. Next time when you run mongodb you need to issue only commands.

1.2 How to install on Ubuntu

Run the following command to import the MongoDB public GPG Key −

Create a /etc/apt/sources.list.d/mongodb.list file using the following command.

Now issue the following command to update the repository −

Now install the MongoDB by using following command −

In the above installation 2.2.3 is currently released mongodb version. Make sure to install latest version always.

Now mongodb is installed successfully.

Start MongoDB:

Stop MongoDB:

Restart MongoDB:

To use mongodb run the following command

This will connect you to running mongod instance.

STEP 2:  How to use query in mongoDB 

  • Design your schema according to user requirements.
  • Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins).
  • Duplicate the data (but limited) because disk space is cheap as compare to compute time.
  • Do joins while write, not on read.
  • Optimize your schema for most frequent use cases.
  • Do complex aggregation in the schema


Suppose a user needs a database design for his post related website and see the differences between RDBMS and MongoDB schema design.

Website has the following requirements.

  • Every post has the unique title, description and url.
  • Every post can have one or more tags.
  • Every post has the name of its publisher and total number of likes.
  • Every Post have comments given by users along with their name, message, data-time and likes.
  • On each post there can be zero or more comments.

In RDBMS schema design for above requirements will have minimum three tables.


While in MongoDB schema design will have one collection post and has the following structure:


So while showing the data, in RDBMS you need to join three tables and in mongodb data will be shown from one collection only.

STEP 3: MongoDB Commands

Create Database

MongoDB use DATABASE_NAME is used to create database. This command will create a new database, if it doesn’t exist otherwise it will return the existing database.


Basic syntax of use DATABASE statement is as follows:

Drop Database

MongoDB db.dropDatabase() command is used to drop a existing database.


Basic syntax of dropDatabase() command is as follows:

Create Collection:

MongoDB db.createCollection(name, options) is used to create collection.


Basic syntax of createCollection() command is as follows:

In the command, name is name of collection to be created. Options is a document and used to specify configuration of collection

Parameter Type Description
Name String Name of the collection to be created
Options Document (Optional) Specify options about memory size and indexing

Options parameter is optional, so you need to specify only name of the collection. Following is the list of options you can use:

Field Type Description
capped Boolean (Optional) If true, enables a capped collection. Capped collection is a collection fixed size collecction that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
autoIndexID Boolean (Optional) If true, automatically create index on _id field.s Default value is false.
size number (Optional) Specifies a maximum size in bytes for a capped collection. If If capped is true, then you need to specify this field also.
max number (Optional) Specifies the maximum number of documents allowed in the capped collection.

Drop Collection 

MongoDB’s db.collection.drop() is used to drop a collection from the database.


Basic syntax of drop() command is as follows

Insert Document

To insert data into MongoDB collection, you need to use MongoDB’s  insert() or save()method. save() method work same as a insert() method.


Basic syntax of insert() command is as follows −

Update Document

The update() method updates values in the existing document.


Basic syntax of update() method is as follows

Delete Document

MongoDB’s remove() method is used to remove document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag

  1. deletion criteria: (Optional) deletion criteria according to documents will be removed.
  2. justOne : (Optional) if set to true or 1, then remove only one document.


Basic syntax of remove() method is as follows

Query Document

To query data from MongoDB collection, you need to use MongoDB’s find() method.


Basic syntax of find() method is as follows

find()method will display all the documents in a non structured way.

MongoDB supports many datatypes whose list is given below:

  • String : This is most commonly used datatype to store the data. String in mongodb must be UTF-8 valid.
  • Integer : This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
  • Boolean : This type is used to store a boolean (true/ false) value.
  • Double : This type is used to store floating point values.
  • Min/ Max keys : This type is used to compare a value against the lowest and highest BSON elements.
  • Arrays : This type is used to store arrays or list or multiple values into one key.
  • Timestamp : ctimestamp. This can be handy for recording when a document has been modified or added.
  • Object : This datatype is used for embedded documents.
  • Null : This type is used to store a Null value.
  • Symbol : This datatype is used identically to a string however, it’s generally reserved for languages that use a specific symbol type.
  • Date : This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.
  • Object ID : This datatype is used to store the document’s ID.
  • Binary data : This datatype is used to store binay data.
  • Code : This datatype is used to store javascript code into document.
  • Regular expression : This datatype is used to store regular expression

For more details visit MongoDB website: https://www.mongodb.com

Post by richa

Leave a Reply

Your email address will not be published. Required fields are marked *