Learn MongoDB

Document database that stores flexible JSON-like records at scale

21 lessons · 4 hours · 4 levels · covers MongoDB 8.0

MongoDB is a general-purpose document database: records are JSON-like BSON documents grouped in collections, queried with a rich filter language and an aggregation pipeline. It is popular for web and mobile back ends because documents map naturally to objects in code, and it scales out with replica sets and sharding.

Before you start

MongoDB builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics8 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms14 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs18 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know21 lessons · 4 hours in total

The MongoDB tree

MongoDBDocuments, collections and BSONDocuments,collections and BSONConnecting with mongoshConnecting withmongoshInserting documentsInserting documentsFinding documents and projectionsFinding documentsand projectionsQuery operatorsQuery operatorsSorting, limiting and pagingSorting, limiting andpagingUpdating documentsUpdating documentsDeleting documents and collectionsDeleting documentsand collectionsQuerying and updating arraysQuerying andupdating arraysIndexes and explainIndexes and explainCompound indexes and the ESR ruleCompound indexesand the ESR ruleThe aggregation pipelineThe aggregationpipelineEmbedding vs referencingEmbedding vsreferencingUsing MongoDB from Node.jsUsing MongoDB fromNode.jsObjectsObjectsArraysArraysArray methods: map, filter, reduceArray methods: map,filter, reduceasync and awaitasync and await
Essentials and Practical shown (14 of 21 lessons); the full list is below. Click a lesson to read it.

All 21 lessons, in order

Documents and the shell

  1. Documents, collections and BSON · Essentials · 8 min

    MongoDB stores JSON-like documents in collections inside databases, encoded as typed binary BSON.

  2. Connecting with mongosh · Essentials · 8 min

    Use the MongoDB Shell to connect to a server, switch databases and run commands in JavaScript.

Create, read, update, delete

  1. Inserting documents · Essentials · 7 min

    Add documents with insertOne and insertMany and understand what they return.

  2. Finding documents and projections · Essentials · 9 min

    Read documents with find and findOne, filter by field values and nested fields, and choose returned fields.

  3. Query operators · Essentials · 10 min

    Use comparison, logical and element operators like $gt, $in, $or, $exists and $regex in filters.

  4. Sorting, limiting and paging · Essentials · 8 min

    Order results with sort, page with limit and skip or a range key, and count with countDocuments.

  5. Updating documents · Essentials · 10 min

    Change documents with updateOne and updateMany using $set, $inc, $unset and $push, and upsert when missing.

  6. Deleting documents and collections · Essentials · 6 min

    Remove documents with deleteOne and deleteMany, and drop whole collections when appropriate.

Querying in depth

  1. Querying and updating arrays · Practical · 12 min

    Match array elements with plain equality, $all, $size and $elemMatch, and update elements with positional operators.

Indexes and performance

  1. Indexes and explain · Practical · 12 min

    Create single-field, unique and multikey indexes and use explain to see whether a query scans the collection.

  2. Compound indexes and the ESR rule · Practical · 12 min

    Design multi-field indexes in Equality, Sort, Range order so one index serves filter and sort.

Aggregation

  1. The aggregation pipeline · Practical · 14 min

    Transform and summarise data with pipeline stages like $match, $group, $project, $sort and $limit.

  2. $lookup joins and $unwind · Advanced · 12 min

    Join collections with $lookup and flatten arrays into one document per element with $unwind.

Data modeling and apps

  1. Embedding vs referencing · Practical · 14 min

    Model data around access patterns: embed what is read together and reference what is large, shared or unbounded.

  2. Using MongoDB from Node.js · Practical · 12 min

    Connect with the official Node.js driver, run CRUD with async/await and reuse one MongoClient.

  3. Schema validation · Advanced · 10 min

    Enforce required fields, types and value ranges on a collection with $jsonSchema validators.

Replication, transactions and sharding

  1. Replica sets, read and write concern · Advanced · 14 min

    Understand primary/secondary replication, elections, the oplog and how write and read concerns trade safety for latency.

  2. Multi-document transactions · Advanced · 12 min

    Run ACID transactions across documents and collections with sessions and withTransaction.

  3. Sharding and shard keys · Expert · 15 min

    Scale horizontally by partitioning collections across shards, and choose a shard key that spreads load and targets queries.

Internals

  1. WiredTiger storage engine · Expert · 12 min

    Learn how WiredTiger caches, compresses, journals and checkpoints data and why working set size drives performance.

  2. Query planner and plan cache · Expert · 12 min

    See how MongoDB chooses among candidate plans, caches the winner, and how to diagnose and steer plan selection.