CPCODELAB
MongoDB

What is MongoDB?

8 min read

MongoDB: A Document Database

MongoDB is a NoSQL database that stores data as JSON-like documents instead of rows in tables. Each document is a self-contained unit with flexible structure — fields can vary across documents and data types can change over time.

In a relational database you might have a students table with fixed columns. In MongoDB, each student is a document inside a students collection, and each document can have different fields.

Documents vs Rows

A SQL row looks like a flat list of column values. A MongoDB document looks like a JavaScript object and can nest arrays and sub-documents directly inside it — no joins required for simple cases.

js
// SQL mental model
// id | name  | age | city
// 1  | Aditi | 21  | Delhi

// MongoDB document
{
  _id: ObjectId("64f1a2b3c4d5e6f7a8b9c0d1"),
  name: "Aditi",
  age: 21,
  city: "Delhi",
  skills: ["JavaScript", "Python"],
  address: {
    street: "12 MG Road",
    pincode: "110001"
  }
}

Every document in MongoDB automatically gets a unique _id field (an ObjectId) if you do not supply one yourself.

Ready to test yourself?
Practice MongoDB— quiz & coding exercises