MongoDB
The world's most popular NoSQL database. Store data as flexible JSON-like documents instead of rigid tables. Perfect for modern APIs, real-time apps, and any project where your data structure evolves fast.

db.students.insertOne({
name: "Alice",
age: 20,
grade: "A",
courses: ["Python", "MongoDB"]
})
db.students.find({ age: { $gte: 18 } })
.sort({ grade: 1 })
.limit(5){ acknowledged: true,
insertedId: ObjectId("...") }About MongoDB
Introduction
What is MongoDB?
MongoDB is a document-oriented NoSQL database released in 2009. Instead of storing data in rows and columns like SQL, MongoDB stores each record as a BSON document β a rich, JSON-like structure that can contain nested objects, arrays, and any mix of data types.
This flexibility is MongoDB's superpower. A user document can contain their address, preferences, and order history all in one place β no joins required. When your data schema changes (and it always does in early development), you do not have to migrate a table structure.
MongoDB powers the back-ends of thousands of applications: from startups using the MERN stack (MongoDB, Express, React, Node.js) to large enterprises at Forbes, EA, and Coinbase. It is horizontally scalable, cloud-native via MongoDB Atlas, and has one of the best query languages in the NoSQL world.
SQL
MongoDB
Why learn MongoDB in 2025?
- β#1 most popular NoSQL database for 10+ years running
- βEssential for the MERN stack β the dominant full-stack JavaScript setup
- βMongoDB Atlas makes cloud deployment one click
- βDocument model maps perfectly to how JavaScript objects look
- βAggregation pipeline is powerful enough to replace most SQL analytics
Curriculum
What you will learn
Documents & Collections
MongoDB structure, BSON vs JSON, creating databases and collections, inserting documents.
Querying Documents
find(), findOne(), comparison operators ($gt, $lt, $eq), logical operators ($and, $or, $in).
CRUD β Update & Delete
updateOne(), updateMany(), $set, $inc, $unset, $push, deleteOne(), deleteMany().
Sorting, Limiting & Skip
sort(), limit(), skip(), projection (choosing which fields to return), chaining methods.
Aggregation Pipeline
$match, $group, $sort, $project, $limit, $lookup β the MongoDB equivalent of SQL GROUP BY.
Indexes & Schema Design
createIndex(), compound indexes, unique indexes, embedded vs referenced documents, best practices.
Cheat Sheet
MongoDB Quick Reference
db.collection.insertOne({...})Insert a single documentdb.collection.insertMany([{...},{...}])Insert multiple documentsdb.collection.find({})Get all documents in a collectiondb.collection.find({ age: 25 })Find documents where age equals 25db.collection.findOne({ name: "Alice" })Return first matching document{ age: { $gt: 18 } }Query operator: greater than 18{ $or: [{a:1},{b:2}] }Match documents where a=1 OR b=2db.collection.updateOne(filter, update)Update the first matching document{ $set: { field: value } }Set operator: update a specific fielddb.collection.deleteOne({ _id: id })Delete first matching documentdb.collection.deleteMany({ active: false })Delete all matching documentsdb.collection.countDocuments(filter)Count matching documents.sort({ field: 1 })Sort ascending (1) or descending (-1).limit(10)Return only first 10 results.skip(20)Skip first 20 results (for pagination)db.collection.createIndex({ field: 1 })Create an index on a fieldNeed the full tutorial? Start the beginner course β
Preview
What MongoDB queries look like
// Insert a document
db.products.insertOne({
name: "Laptop",
brand: "TechCo",
price: 12999,
inStock: true,
tags: ["electronics", "computers"],
specs: {
ram: "16GB",
ssd: "512GB"
}
})
// Find all products under R15000
db.products.find(
{ price: { $lt: 15000 } },
{ name: 1, price: 1, _id: 0 }
).sort({ price: 1 }){ acknowledged: true, insertedId: ObjectId("...") }
[
{ name: "Mouse", price: 299 },
{ name: "Laptop", price: 12999 }
]// Aggregation pipeline:
// Average score per city
db.students.aggregate([
{
$match: { grade: { $in: ["A", "B"] } }
},
{
$group: {
_id: "$city",
avgScore: { $avg: "$score" },
count: { $sum: 1 }
}
},
{
$sort: { avgScore: -1 }
},
{
$project: {
city: "$_id",
avgScore: { $round: ["$avgScore", 1] },
count: 1,
_id: 0
}
}
])[
{ city: "Cape Town", avgScore: 91.3, count: 3 },
{ city: "Johannesburg", avgScore: 80.5, count: 2 }
]FAQ
Frequently asked questions
What is the difference between MongoDB and SQL?
SQL databases store data in fixed tables with rows and columns β every row must follow the same schema. MongoDB stores data as flexible JSON-like documents β each document can have different fields. SQL is great for structured relational data; MongoDB shines for hierarchical, flexible, or rapidly changing data structures.
What is a document in MongoDB?
A document is a record in MongoDB, stored in BSON (Binary JSON) format. It looks like a JavaScript object with key-value pairs. Documents can contain nested objects and arrays, making them ideal for representing complex real-world entities in a single record without joins.
What is a collection in MongoDB?
A collection is a group of documents β roughly equivalent to a table in SQL. Unlike SQL tables, collections do not enforce a fixed schema, so different documents in the same collection can have completely different fields.
When should I use MongoDB instead of SQL?
Use MongoDB when your data is hierarchical or document-like (user profiles with embedded addresses, product catalogues with varying attributes), when your schema changes frequently, or when you need horizontal scaling. Use SQL when you have complex relationships between entities, need strict ACID transactions across multiple tables, or are building financial systems.
What is the aggregation pipeline?
The aggregation pipeline is MongoDB's way to process and transform documents in stages β similar to SQL's GROUP BY, JOIN, and HAVING clauses. Each stage takes the output of the previous stage. Common stages include $match (filter), $group (aggregate), $sort, $project (reshape), and $lookup (join collections).
Does MongoDB support transactions?
Yes β since version 4.0, MongoDB supports multi-document ACID transactions. For single-document operations, MongoDB has always been atomic. Multi-document transactions are useful when you need to update several documents and either all changes succeed or none do.
Ready to learn MongoDB?
6 modules, 18 examples with full output. Go from zero to writing real queries, aggregations and indexes.
The Story of MongoDB
Real history, real companies, and an honest look at where MongoDBshines and where it doesn't β not just a feature list.
Where it came from
MongoDB was created in 2007 by the founders of DoubleClick (later acquired by Google), who had first-hand experience with the scaling limitations of relational databases at internet scale. They designed MongoDB around flexible, JSON-like documents instead of rigid tables, aiming to make it easier to change your data's shape as an application evolves.
How itβs actually used today
MongoDB is used by eBay for parts of its catalogue system, by Forbes for content management, and broadly across the startup world for rapid product development, since its schema-less design lets teams change how they store data without a formal database migration. It's especially common paired with Node.js in what's sometimes called the MEAN or MERN stack.
Strengths & trade-offs
Flexible schemas make MongoDB genuinely fast to prototype with, since you don't need to lock in your data structure before you start building β but that same flexibility can bite teams later, when inconsistent document shapes accumulate across years of changes and nobody enforced a consistent structure early on.
What to learn next
Node.js is the most common pairing for MongoDB in real projects, since both share JSON as a natural data format. It's also genuinely useful to learn SQL alongside MongoDB rather than instead of it β most experienced engineers end up choosing between the two per project, not picking one forever.