What is MongoDB?

MongoDB is an open-source, document-oriented database designed for modern applications. It stores related data in flexible documents, making it useful when an application needs to evolve quickly, handle high traffic, or scale across multiple servers.

MongoDB stores documents in JSON-like BSON format. A database contains collections (similar to tables in a relational database), and collections contain documents (similar to rows). Documents in the same collection do not have to contain exactly the same fields, although applications should still validate important data and use indexes for common queries.

How MongoDB Stores Data in a Document


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

This document embeds the address inside the user record. In a relational design, the same data might be split between user and address tables and connected with a key. Embedding is convenient for data that is usually read together; related data can also be referenced when it changes independently or grows without bounds.


NoSQL

NoSQL is a broad category of database systems that do not depend on the traditional relational table model. The name is commonly expanded as "not only SQL": many NoSQL databases support their own query languages, and some also support SQL-like queries.

NoSQL databases can store structured, semi-structured, and relationship data. MongoDB represents relationships with embedded documents or references, depending on how the application reads and updates the data.

NoSQL systems became popular for workloads that benefit from flexible data models, low-latency access, and horizontal scaling. They are not automatically faster or better than relational databases, so the workload and consistency requirements should guide the choice.

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 supportSupportedSupported by MongoDB transactions
JoinsCommonly usedOptional; MongoDB uses embedded documents, references, and $lookup
Data to Object MappingRequires ORM i.e. (object-relational mapping)Not Required in MongoDB

SQL to NoSQL

Developers familiar with JavaScript often find MongoDB's JSON-like documents approachable. Developers coming from a relational database will need to learn how collections, documents, indexes, aggregation pipelines, and embedding differ from tables, rows, joins, and SQL queries.

Use this SQL to MongoDB cheatsheet as a starting point, then consult the linked lessons for complete examples.

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 (name) VALUES ('avinash')db.employee.insertOne({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 current MongoDB shell for connecting to a server
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

MongoDB is a good fit when the application works with nested records, needs a flexible document model, or must distribute read and write traffic across servers. Common examples include:

  1. Big Data
  2. Content Delivery Management
  3. Unstructured Data
  4. Real Time Analytics
  5. IoT
  6. Mobile Apps
  7. Web Applications

When to choose MongoDB

Choose MongoDB when data is naturally represented as a document, access patterns are known, and the application benefits from embedding related values. Plan indexes around real queries, validate required fields, and use transactions when an operation must update multiple documents atomically.

A relational database may be a better fit for heavily normalized data, complex joins, strict schemas, or reporting workloads that depend on mature SQL tooling. Choose the database based on the data model, consistency needs, and query patterns rather than popularity alone.


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