Introduction to MongoDB
What MongoDB is, why NoSQL, and when to use it
Skills taught: mongodb.basics, mongodb.concepts
What is MongoDB?
MongoDB is a document-oriented NoSQL database that stores data as flexible JSON-like documents called BSON. Unlike relational databases, MongoDB collections do not enforce a fixed schema — each document can have different fields and data types.
Founded in 2009, MongoDB is now one of the most popular databases in the world, used by companies like Adobe, eBay, LinkedIn, and Uber. It excels at storing hierarchical, unstructured, or semi-structured data that doesn't fit neatly into rows and columns.
MongoDB vs Relational Database (SQL)
- MongoDB (NoSQL): Document-oriented, schema-flexible database
- SQL (Relational): Table-based, schema-enforced database
Core Concepts
- Document
- A JSON-like record stored in MongoDB. Equivalent to a row in SQL but with no fixed columns. Documents are stored as BSON (Binary JSON) internally.
- Collection
- A group of documents, like a SQL table but schema-free. All documents in a collection are related by convention, not enforced by the database.
- Database
- A container for collections. One MongoDB server can host many databases, each completely isolated.
// A MongoDB document — equivalent to a SQL row
// but with nested objects and arrays natively supported
{
_id: ObjectId("507f1f77bcf86cd799439011"), // auto-generated unique ID
name: "Alice",
age: 30,
email: "alice@example.com",
tags: ["admin", "user"], // arrays natively supported
address: { // nested objects (embedded documents)
city: "Mumbai",
state: "MH",
zip: "400001"
},
createdAt: ISODate("2026-01-15T10:00:00Z")
}
// Another document in the SAME collection — different fields!
{
_id: ObjectId("507f1f77bcf86cd799439012"),
name: "Bob",
age: 25,
// No email, no address — that's perfectly valid in MongoDB
preferences: { theme: "dark", language: "en" }
}
Tip: MongoDB auto-creates a collection when you first insert into it. You don't need to define the schema before writing data — perfect for rapid prototyping.
SQL vs MongoDB Terminology
- SQL Table → MongoDB Collection
- SQL Row → MongoDB Document
- SQL Column → MongoDB Field
- SQL Primary Key → MongoDB _id
- SQL Foreign Key → MongoDB Reference (by _id)
- SQL JOIN → MongoDB $lookup (aggregation stage)
MongoDB stores documents in BSON (Binary JSON), which extends JSON with additional types like Date, ObjectId, Binary, Decimal128, and regular expressions. This makes MongoDB more precise than plain JSON for data storage.
Frequently Asked Questions
When should I use MongoDB over SQL?
MongoDB excels when your data is hierarchical or when the schema changes frequently. Use it for product catalogs, user profiles, event logs, and content management. Use SQL for complex joins, enforced relational integrity, and strict ACID transactions across multiple tables.
Is MongoDB truly schema-less?
MongoDB is schema-flexible by default, but you can enforce a schema using JSON Schema validation rules on a collection. In practice, most teams define an implicit schema through their application code or use Mongoose to enforce it at the ODM layer.
Does MongoDB support transactions?
Yes. Since MongoDB 4.0, multi-document ACID transactions are supported. Single-document operations have always been atomic. For most use cases, single-document atomicity is sufficient.