What is MongoDB?

Mongo DB is an open source, document-based database based on No SQL for modern applications and cloud based servers. MongoDB is used to build high volume, scalable and performance databases

A document-based or document-oriented database like Mongo DB use JSON like key-value pairs to store data. Mongo DB use collections instead of tables. Each database contains collections ( table in RDBMS ) which contains documents ( columns in RDBMS ). The number of documents in each documents can be different from others.

How Mongodb Stores data in JSON


    {
        "_id"="5eab0997f687e3ea1b1ea997",
        "name":"user",
        "id":"212",
        "age":"22",
        "address":{
                "city":"new delhi",
                "pin":"110001" 
               }
    }

In above example, MongoDB is using one document, i.e.{} to store all data, but for the same query, RDBMS will create two tables, one for user and another for address.


NoSQL

NoSQL ( Not only SQL ) is used to create Non-relational databases , like MongoDB, CouchDB, Redis, Oracle NoSQL. Some blogs describe NoSQL as Non SQL, but actually it is Not Only SQL.

Some people also says that NoSQL cannot store relationship data. But this term is wrong. Actually NoSQL stores relationship data but in another form than sql.

NoSQL databases were developed in 2000. NoSQL focus on scalability, performance and simplicity.

NoSQl Vs SQL

NoSQL Vs SQL
SQL NoSQL
Type Relational Non Relational
Schemas Pre Defined Dynamic
Base Table key-value, document, graph
Scability Vertical Horizontal
ACID SupportYesNo ( except Mongo DB)
JoinsRequiredNot Compulsory
Data to Object MappingRequires ORM i.e. (object-relational mapping)Not Required in MongoDB

SQL to MongoDB

MongoDB was developed in 2000. JavaScript Developers will learn MongoDB very easily as MongoDB is JSON based. But RDBMS developer might find MongoDB little diffucult to understand.

Here is a cheatsheet for SQL to mongoDB.

SQL to MongoDB Cheatsheet
SQLMongoDB
Database Database
TableCollection
RowDocument
ColumnField
IndexIndex
Joins$lookup
SELECT * from tablenamedb.collections.find()
SELECT * from tablename LIMIT 1db.collections.findOne()
CREATE TABLE employee ( name VARCHAR(100))db.createCollection('employee')
INSERT INTO employees VALUES ( avi, 212)db.employees.insert({name:'avi',id:212})


Mongodb advantages

The Advantage of Mongodb over RDBMS is that Mongodb is document based, schema less, fast and scalable.

  1. MongoDB is document database. Each collection can have as many documents to store data.
  2. Schema less database. No need to define length of data and datatype.
  3. Scalable very easy to scale.
  4. Document Oriented database that stores data in JSON or BSON
  5. Joins support, but not compulsory.
  6. Powerful Query language.
  7. Full ACID transactions.

MongoDB Commands in Mongo shell

MongoDB Commands in Mongo shell
Command Use
mongod command to start mongodb server
mongo open test database in command prompt
show dbsshow all available Databases
use mydbselect mydb Database. If mydb does not exists, it will create mydb
show collectionsshow all collections in database selected
dbcheck current database
db.mydb.help()show all methods used with database
db.dropDatabase()to drop existing database

Where to use MongoDB

Here are the primary areas where MongoDB is preferred.

  1. Big Data
  2. Content Delivery Management
  3. Unstructured Data
  4. Real Time Analytics
  5. IoT
  6. Mobils Apps

Companies Using MongoDB

  1. Facebook
  2. Google
  3. Adobe
  4. Ebay
  5. SAP
  6. Electronics Arts
  7. GOV.UK