What is MongoDB?

Mongo DB is an source available, document-oriented database based on No SQL for modern web 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 query language used to create both relational and non-relational databases, like MongoDB, CouchDB, Redis, Oracle NoSQL. Some blogs describe NoSQL as Non SQL, but actually NoSQL 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 can also store non-relational data. This means, NoSQL supports both structured and unstructured data.

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
Scalability 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 NoSQL

MongoDB was developed in 2000. JavaScript Developers will learn MongoDB very easily as MongoDB is JSON based. But RDBMS developer might find MongoDB a bit different then traditional databases (SQL based).

Here is a cheatsheet for SQL to NoSQL.

SQL to NoSQL 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(20))db.createCollection('employee')
INSERT INTO employee VALUES ( avinash)db.employees.insert({name:'avinash'})


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 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.
  8. Both Cloud and on-premises support.

MongoDB Commands in Mongo shell

MongoDB Commands in Mongo shell
Command Use
mongod command to start mongodb server
mongo open db in terminal
mongosh latest shell for Mongodb 6 and above
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 or delete 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. Mobile Apps
  7. Web Applications

Companies Using MongoDB

Here is a list of popular companies using MongoDB Databases in their applications.

  1. Facebook
  2. Google
  3. Adobe
  4. Forbes
  5. Toyota
  6. Ebay
  7. SAP
  8. Electronics Arts
  9. GOV.UK
  10. GE HealthCare