Aggregation Pipeline
As a Engineering Manager engineer, explain Aggregation Pipeline. What problem does it solve and how would you describe it in an interview?
Answers use simple, clear English.
Quick interview answer
At Engineering Manager depth: start with the problem, then mechanism, then a short example. Stages: $match → $group → $sort → $lookup (join) → $project.
Detailed answer
At Engineering Manager depth: start with the problem, then mechanism, then a short example. Stages: $match → $group → $sort → $lookup (join) → $project. Pipeline runs server-side; push filters early in $match for index use. Core: Stages: $match → $group → $sort → $lookup (join) → $project. Pipeline runs server-side; push filters early in $match for index use. Real-time example: Monthly revenue by category: $match date range, $unwind items, $group by category sum price. Pros: Powerful analytics in database; reduces app-side processing. Cons: $lookup expensive on large collections without indexes; memory limits on sort/group. Common mistakes: $lookup before $match blowing working set; missing allowDiskUse on huge sorts. Best practices: Index fields in initial $match; $project early to trim fields. Audience level: Engineering Manager.
Full explanation
Stages: $match → $group → $sort → $lookup (join) → $project. Pipeline runs server-side; push filters early in $match for index use.
Real example & use case
Monthly revenue by category: $match date range, $unwind items, $group by category sum price.
Pros & cons
Pros: Powerful analytics in database; reduces app-side processing. Cons: $lookup expensive on large collections without indexes; memory limits on sort/group.
Common mistakes
$lookup before $match blowing working set; missing allowDiskUse on huge sorts.
Best practices
Index fields in initial $match; $project early to trim fields.
Follow-up questions
- How would you test Aggregation Pipeline?
- What metrics prove Aggregation Pipeline is healthy in prod?
- How does Aggregation Pipeline change at 10× traffic?