Monday, November 28, 2016

MongoDB basic Overview


  • MongoDB is a cross-platform, documented oriented database and it is not based on schema like relational database.
  • It uses dynamic schema and stores data in JSON format.
  • It provides high performance, high availability, and easy scalability and it works on concept of collection and document.
  • It is an open-source software.
  • MongoDB is mainly written in C++, JavaScript and C.


Please refer this link to setup MongoDB on your machine.


·         Document

Document is similar to row/tuples in RDBMS, it is a set of key-value pairs and having dynamic schema i.e. the documents in the same collection do not need to have the same set of fields or structure and another document may hold different types of data.

·         Collection

It is the equivalent to a TABLE in RDBMS and do not enforce a schema. It exists within a single database and each document within a collection can have different schema.

·         Database

It is the equivalent to a DATABASE in RDBMS and a database can have zero or more collections.

Sample Document

{ "_id" : ObjectId("583c623e2226aa5f3b8e14f4"),
"title" : "MongoDB Basic Overview",
"by" : ""

Where _id is the unique key for each document generated by MongoDB. Even we can provide unique key in MongoDB by specifying _id attribute while inserting data.
The generated unique key has specific format i.e. first 4 bytes are for the current timestamp, next 3 bytes are for machine id, next 2 bytes are for process id of MongoDB server and last 3 bytes are simple incremental VALUE. 

Advantages of MongoDB

· It is a schema less document
· No more complex joins
· Provides ACID properties at the document level as in the case of relational databases.
· Supports common authentication mechanisms, such as LDAP, AD, and certificates. Users can connect to MongoDB over SSL and the data can be encrypted.
· Enables horizontal scalability by using a technique called Sharding.
· Supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
· Conversion/mapping of application objects to database objects not needed.
· Cost effective solution as it improves flexibility and reduces cost on hardware and storage.
· Supports replica sets i.e. a failover mechanism is automatically handled. If the primary server goes down, the secondary server becomes the primary automatically, without any human intervention.

When to Use MongoDB Rather than MySQL or any Other RDBMS

· When your data is going to big and schema is not defined.
· When your data is location based
· When you expect a high load
· When you need to partition and shard your database.
· When you want to create, replica set (set of servers that act as Master-Slaves) 


MongoDB is great tool and can be very useful in creating applications like bug tracking, discussion forums, advertisements, and the like. However, Joins are not possible in MongoDB; It requires proper analysis before making a decision.

Sunday, November 27, 2016

How to install and verify MongoDB on Windows 7 ?

Step by Step instructions:

1.      Download MongoDB

Check MongoDB msi from Official website and download Windows Server 2008 R2 64-bit and later version.

2.      Install MongoDB

Double click on downloaded MSI (mongodb-win32-x86_64-2008plus-ssl-3.2.11-signed.msi) file and follow the click on next button on wizard to complete the installation.

3.      Create Default Directory

MongoDB requires a data directory to store all data and its default data directory path is \data\db.
On Windows, By default it will always look for above directory under C:/ structure, Goto C: directory and create /data/db, So the full path of db folder will be C:\data\db

4.      Set environment variables

Set MongoDB location to system's environment variables and give path till bin folder.


5.      Start MongoDB

To start MongoDB server, Open cmd and type mongod, it will start the MongoDb server on port -27017.


6.      Verify MongoDB

Open cmd prompt and type mongo.exe

Additional Notes:

 1.      Configuration File

We can create a config file where we can add the custom path of data/db directory, Config file will be similar to properties file and add attributes in key/value pair.
##data path can be updated using dbpath

##log file

Use mongod.exe –config E:/mongodb/mongo.config to load config file while starting mongodb2. 

2.      Start as a service

We can start mongoDB as a service, Adding MongoDB as Windows Service will start MongoDB automatically following each system restart. Install as Windows Service with --install.
E:\mongodb\bin> mongod --config E:\mongodb\mongo.config --install

To start MongoDB Service
net start MongoDB

To stop MongoDB Service
net stop MongoDB

To remove MongoDB Service

d:\mongodb\bin>mongod --remove

Thank you..!!!