Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

MDB
MongoDB
0/10 completed
Beginner
What is MongoDB?Inserting and finding documentsUpdating and deleting
Intermediate
Query operatorsSorting, paging and projectionIndexes and explain()
Advanced
The aggregation pipelineJoins, facets and window functionsSchema design and validation
Pro
Drivers, transactions and scaling
Tracks / MongoDB / Beginner

What is MongoDB?

MongoDB is a document database. Instead of rows in tables, it stores documents: JSON-like objects that can contain nested objects and arrays. It's one of the most popular databases in the world, especially for JavaScript and Node.js developers.

Documents instead of rows

In a relational database, a customer with two addresses and three tags needs three tables joined together. In MongoDB, it's one document:

js
{
  _id: ObjectId('65a475806f1c2e9a4b3a91c0'),
  name: 'Asha Rao',
  email: 'asha@example.com',
  age: 31,
  address: { city: 'Bengaluru', country: 'India' },   // a nested object
  tags: ['vip'],                                      // an array
  joined: ISODate('2024-01-15T00:00:00.000Z')
}
Relational (SQL) MongoDB
Database Database
Table Collection
Row Document
Column Field
Primary key _id (added automatically)
JOIN Embedding, or $lookup
Fixed schema (ALTER TABLE) Flexible schema: documents in one collection can differ

Under the hood, documents are stored as BSON (binary JSON), which adds types JSON doesn't have: dates, ObjectId, 64-bit integers, decimals and binary data. A single document can be up to 16 MB.

When MongoDB is a good fit

✅ Data that is naturally a nested "thing" you load and save as a whole (a product with variants, a blog post with comments, a user profile with settings) ✅ Fields that vary between records (a catalogue where TVs and T-shirts have different attributes) ✅ Fast iteration while the shape of your data is still changing ✅ Huge scale: MongoDB was designed to spread data across many servers (sharding)

❌ Heavily relational data with many-to-many links and complex reports often fits SQL better. ❌ "Schemaless" doesn't mean "no design": you still need to think about your schema (lesson 9).

The MongoDB shell, in your browser

mongosh is the official command-line shell. You type JavaScript into it. The editors in this track run a mongosh-style shell simulated in your browser: queries, updates, aggregation pipelines, indexes and validation behave like a real server, and output is formatted like mongosh. Each editor starts with a shop database:

MongoDB shell (simulated) · editable

Each line is run in turn and its result is printed, just like typing it into mongosh. db is the current database:

MongoDB shell (simulated) · editable

ObjectId('…') is MongoDB's default id type, and ISODate('…') is how mongosh shows dates.

💡 The simulator implements the parts of MongoDB this track teaches. A real server also has networking, users, replication and much more, which you'll meet in the last lesson.

Installing MongoDB for real

Atlas (cloud, free tier): create a free cluster at mongodb.com/atlas, and it gives you a connection string. This is the easiest way to start.

Docker:

bash
docker run --name mongo -p 27017:27017 -d mongo:8
docker exec -it mongo mongosh

macOS: brew tap mongodb/brew && brew install mongodb-community

Install mongosh and MongoDB Compass (the official GUI) from mongodb.com. Connect with a connection string:

mongodb://localhost:27017                                  # local
mongodb+srv://user:password@cluster0.abc12.mongodb.net/    # Atlas

Practice

  1. Show one product with db.products.findOne().
  2. Count the orders.
  3. Find the customers whose city is Mumbai. (Hint: { 'address.city': 'Mumbai' }. Quotes are needed around a dotted field name.)
MongoDB shell (simulated) · editable
✎ Check your understanding

1.What is the MongoDB equivalent of a table?

2.What does MongoDB add to every document if you don't provide it?

3.Which data is a natural fit for one MongoDB document?