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 / Advanced

Joins, facets and window functions

The aggregation pipeline can do almost anything a SQL report can: join collections, compute several reports in one pass, bucket values into ranges, and calculate running totals and rankings.

$lookup: joining collections

Orders store a customerId. $lookup fetches the matching customer documents into an array field:

MongoDB shell (simulated) · editable

customer is an array (a lookup can match many documents). When you expect exactly one, $unwind it, then pick the fields you want:

MongoDB shell (simulated) · editable

Top customers by spend

Group first (fewer documents), then look up names, which is much cheaper than looking up every order:

MongoDB shell (simulated) · editable

Customers with no orders

A lookup that finds nothing gives an empty array, which makes "anti-joins" easy:

MongoDB shell (simulated) · editable

⚠️ $lookup is useful, but if you find yourself joining on every request, the data might belong embedded in one document. That's the next lesson's topic.

$facet: several reports in one query

$facet runs several sub-pipelines on the same input and returns all the results together. It's perfect for search pages that show results plus counts per filter:

MongoDB shell (simulated) · editable

$bucket: grouping into ranges

MongoDB shell (simulated) · editable

$bucketAuto picks boundaries for you, given a number of buckets.

Window functions: $setWindowFields

Window functions compute values across related documents without collapsing them into groups: running totals, rankings, moving averages.

Running total

MongoDB shell (simulated) · editable

window: { documents: ['unbounded', 'current'] } means "from the first document up to this one".

Ranking within groups

partitionBy restarts the calculation for each group, like PARTITION BY in SQL:

MongoDB shell (simulated) · editable

That's "the most expensive product in each category" in one pipeline.

Working with arrays inside documents

You don't always need $unwind. Array expressions work on each document's arrays directly:

MongoDB shell (simulated) · editable

Inside $filter and $map, $$i refers to the current element (a variable named by as).

Practice

  1. Join orders to customers and compute total revenue per country.
  2. Use $facet to return, in one query, the number of orders per status and the 2 most recent orders.
  3. Rank customers by age within each country using $setWindowFields.
MongoDB shell (simulated) · editable
✎ Check your understanding

1.What does $lookup add to each document?

2.Which stage computes several independent reports over the same input in one query?

3.How do window functions differ from $group?

← The aggregation pipeline