Getting Started With MongoDB

Learn Mongo db

  • Basic Overview
  • SetUp environment/Installation
  • DataTypes
  • Schema Design
  • Create/Drop Database
  • Create/Drop Collection
  • Insert/update/delete document
  • Query on Document
  • Indexing, limit and sorting
  • Backup and restore database

Basic Overview

MongoDB is an open-source/cross platform (Windows, Linux etc) document database that is a NoSql JSON format database. A NoSQL database means it does not have any relationships (non-relational database). It is a document-oriented database.

Like JSON format it is written in C++. The purpose of this database is to create and deploy a highly scalable and performance-oriented database. Stable release: 3.4.6 / 5 July 2017; 30 days ago Initial release: February 2009, 11

Compare with Relational DB

 Relational DBMongo DB
1Database is called DatabaseDatabse
2Table is called tableCollection
3Any records are called rowDocument
4Column is called columnField
5Table Join with resulting data between two or more tables is called table joinEmbeded Documen
6Primary Key is unique and not null valuePrimary Key (Default key _id) it is hexadecimal value (12 byte)12 byte: 4 byte time stamp, 3 byte machine code, 2 bytes process id and 3 byes for incremeted process 

SetUp environment/Installation 

Download path

Install properly, after installation you will see:

C:\Program Files\MongoDB\Server\3.4\bin,

Then from here you need to do -> Shift with right click,

Mongo db
Now click on Open command window here. And type  mongod.exe.

I hope you will see below Window.

Mongo db

Now you will see the bottom number waiting for connection.

So now again do it with right shift click as a given place. And type mongo.exe.

Remember - do not close first command window

You will  see connection has been accepted/ in first cmd window

Mongo db

Mongo db

Now your installation has been completed.

For  your confirmation just type : show dbs command,

You will see your default database names; for me it shows a few database names,

Mongo db

In this window admin and local is default database, and I have created Demo, MovieStore, and School.


Now we will learn about datatypes in mongo db

MongoDB accepts many datatypes. There are following datatypes, 



ObjectIds are unique, fast to generate, and ordered. ObjectId values consist of 12 bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation. Specifically,

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • a 2-byte process id, and
  • a 3-byte counter, starting with a random value.
Schema Design

In MongoDB, data is a flexible schema. It is we can say different from SQL database or any other relational database, where you need to determine and declare a table's schema before inserting data. MongoDB collections/tables do not force you  to create document structure/schema.

Let me explain with a simple example,

Suppose you have a client who needs a database design for his website.

Suppose he wants a website with the following requirements,

  1. Every post must be distinct (and it contains unique title, description and url).
  2. Every post can have zero, one, or more tags.
  3. Every post can have the name of its publisher and total number of likes/dislikes.
  4. And for every post it can have zero or more comments and the comments must contain its user name, message, data-time and likes/dislikes.

So for the above requirement in relational database,we need minimum three tables. 

Mongo db

But you can see in MongoDB, how much it fulfills the requirement in one collection post and has the following structure:

  1. {  
  2.     _id: int  
  3.     title: string  
  4.     description: string  
  5.     by: string  
  6.     url: string,  
  7.     tags: [],  
  8.     Totallikes: int,  
  9.     TotalDislikes: int,  
  11.             user: string,  
  12.             message: Text,  
  13.             datecreated: DateTime  
  14.             like: int,  
  15.             dislikes: int,  
  16.         }, {  
  17.             user: string,  
  18.             message: Text,  
  19.             dateCreated: DateTime  
  20.             like: : int,  
  21.             dislikes: int  
  22.         }  
  23.     }  
  24. }  
Create/Drop Database

This following command is used to create a new database.



If the database already exists/created, it will return the existing database.

In the example to demonstrate how a database is created in MongoDB, in the following example, we are going to create a database "appeal".

then use the following command

>use appeal

Swithched to db apeal

To check the currently selected database, use the command db:



To check the database list, use the command show dbs:

AS I have already explained in the above window

>show dbs 

In my window i have a few db.

  • demo,
  • moviestore,
  • school,
  • admin,
  • appeal,
  • local

Here, your created database "appeal "is not present in the list, so that you need to insert at least one document into it to display database,


WriteResult({ "nInserted": 1})

Mongo db

For the drop database which you have created this command is used to drop a database. It also deletes the associated data files. It operates on the current database.



This syntax will delete the selected database. In the case of if you have not selected any database, it will delete the default "test" database.

Mongo db

Please see the error, because I did not write function name as case sensitive

So please use proper case for that.

Similar Articles