MongoDB aggregation pipeline in depth
Using the stages and expression operators of the aggregation pipeline in depth, you produce reports, window calculations, time series analyses and data transformations.
Level 301 · 17 lessons · 15 practice labs
Lessons
Stages
- $match, $project and $addFields
- $group and accumulators
- Working with arrays: $unwind, $filter, $map
- $lookup with a pipeline
- Stage order and performance
- Short task: a sales report by category
Expressions
- Conditional expressions: $cond and $switch
- Date expressions and time zones
- String expressions and regular expressions
- Window calculations with $setWindowFields
- Filling gaps with $densify and $fill
- Short task: a weekly user retention report
Output and operations
- $out, $merge and views
- Pre-computed summary collections
- Memory limits and spilling to disk
- Reading an aggregation’s plan
- Capstone: an operations dashboard for a delivery company
Practice labs
- Count the items in each basket: Add each basket's line and quantity counts as new fields using $size and $sum.
- Separate discounted lines with $filter: Without unwinding, pick out discounted lines with $filter and compute net amounts with $map.
- Fix a report inflated by $unwind: Find a pipeline that gives wrong totals because it unwinds two arrays in a row and rewrite it with array expressions.
- Add the branch name to shipment records: Bring the branch name into shipment documents from the branches collection with a simple $lookup.
- Join with conditions: Use a pipeline $lookup to attach only each customer's undelivered orders from the last 30 days.
- Speed up a slow join by reordering stages: Reorder a pipeline that filters after $lookup, index the joined side and measure the time.
- Assign a segment by order total: Split customers into bronze, silver and gold segments by spend with $switch.
- Report by day in the right time zone: Write a report that groups UTC order times into days in Istanbul time and show the midnight shift.
- Extract fields from free text: Pull the tracking code and city name out of shipment notes with $regexFind and write them to separate fields.
- Compute a moving average: Add a 7-day moving average to daily sales with $setWindowFields.
- Fill missing days for a chart: Add days without sales with $densify, set them to zero with $fill and produce a gap-free time series.
- Compute cohort retention: Compute in one pipeline how many users return in the four weeks after their sign-up week, by cohort.
- Save a report query as a view: Define the frequently used monthly sales pipeline as a view and expose only the view to the app.
- Update a summary collection incrementally with $merge: Write a pipeline that processes only yesterday's orders each night and updates a daily summary with $merge.
- Fix an aggregation that hits the memory limit: Read the plan of a large $group that fails on the memory limit, rework the stages and measure spilling to disk.
About this course
Using the stages and expression operators of the aggregation pipeline in depth, you produce reports, window calculations, time series analyses and data transformations.
What you will learn
- Stages
- Expressions
- Output and operations
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal in the browser, connected to a private lab network. Nothing to install.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.