MongoDB Aggregation Pipeline: $match, $group, $lookup
Fast recall
Quick revision
Core answer
_id expression with accumulators like $sum, $avg, $push, $addToSet. `$lookup` performs a left-outer join against another collection, producing an array that $unwind flattens for 1:1 matches. `$project` / `$addFields` reshape output. In Spring Data MongoDB you compose pipelines with the fluent Aggregation builder (match, group, lookup, unwind, project) executed via MongoTemplate.aggregate(). Pipeline order drives performance — reduce cardinality early; large sorts and groups need allowDiskUse(true) past 100MB.Ability to compose non-trivial server-side aggregations and understand stage ordering for performance
Placing `$match` after `$group`/`$lookup`, forgetting to `$unwind` after `$lookup`, or pulling documents to Java to aggregate in memory
Name the 100MB sort spill, mention `allowDiskUse`, `$facet` for parallel pipelines, and `$merge` / `$out` for materialized-view output
Interview-ready guide
Interview answer
Frame the pipeline
- Sequence of stages; documents stream through
- Each stage is a function from stream to stream
Cover the core stages
$matchfirst — uses indexes$group— collapses by_idwith accumulators$lookup— left-outer join, result arrives as array,$unwindflattens$project/$addFields— reshape output
Emphasize ordering
- Reduce cardinality early
- Heavy stages last on smallest possible input
Mention the operational realities
allowDiskUse(true)for large pipelines$merge/$outfor materialized views- Indexes on foreign fields for
$lookup
Close with the Spring Data builder
Aggregation.newAggregation(match, group, lookup, unwind, project, sort, limit)- Executed via
mongoTemplate.aggregate()
MongoDB Aggregation Pipeline: $match, $group, $lookup
Direct Answer
The aggregation pipeline is a sequence of stages that transform documents as they flow through — Unix pipes for BSON. $match filters early (use indexes when it's the first stage). $group collapses documents by an _id expression with accumulators like $sum, $avg, $push, $addToSet. $lookup performs a left-outer join against another collection, producing an array that $unwind flattens for 1:1 matches. $project / $addFields reshape output. In Spring Data MongoDB you compose pipelines with the fluent Aggregation builder (match, group, lookup, unwind, project) executed via MongoTemplate.aggregate(). Pipeline order drives performance — reduce cardinality early; large sorts and groups need allowDiskUse(true) past 100MB.
Common Mistakes & Pitfalls
Placing $match after $group/$lookup, forgetting to $unwind after $lookup, or pulling documents to Java to aggregate in memory
Check your understanding
Answer these without looking back at the article.
- When would you pick `$facet` over running two separate pipelines?
- How does `$merge` differ from `$out` for materializing results?
- How do you paginate over an aggregation pipeline reliably?
- Why does `$lookup` without a supporting index on the foreign field perform so poorly?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.