MongoDB Tutorial 0/120 lessons ~6 min read Lesson 108
Aggregation Challenges
Aggregation pipelines are the highest-leverage skill in MongoDB.
Course progress0%
Focus
4 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Aggregation pipelines are the highest-leverage skill in MongoDB. These challenges progress from a one-stage $group through to a multi-stage $facet with $lookup and window functions — the exact shapes you'll write in dashboards, exports, and ETL.
Understanding the topic
10 progressive aggregation challenges:
- 1. Group orders by month → revenue & count.
- 2. Top 10 customers by lifetime spend.
- 3.
$lookuporders → customer profile. - 4.
$unwindline items, then top-selling SKU. - 5. Rolling 7-day active users with
$setWindowFields. - 6.
$facetKPI dashboard in one query. - 7. Funnel analysis — events → conversion %.
- 8. Cohort retention by signup month.
- 9. Materialise a daily rollup with
$merge. - 10. Geo aggregation — top zip codes by revenue.
Informative example
Challenge 5 — rolling 7-day active users:
js
db.events.aggregate([{ $match: { type:"login", ts:{ $gte: ISODate("2025-01-01") } } },{ $group: { _id: { day: { $dateTrunc: { date:"$ts", unit:"day" } }, userId:"$userId" } } },{ $group: { _id: "$_id.day", dau: { $sum: 1 } } },{ $setWindowFields: {sortBy: { _id: 1 },output: { wau: { $sum: "$dau", window:{ range:[-6,0], unit:"day" } } }} },{ $sort: { _id: 1 } }]);
Best practices
- Build pipelines stage-by-stage; preview with
{ $limit: 5 }after each addition. - Move
$match+$sortas early as possible — they use indexes. - Persist heavy pipelines with
$mergeif dashboards re-run them often.
Ready to mark this lesson complete?Track your journey across the entire course.