Aggregation Pipeline
What common mistakes do candidates make around Aggregation Pipeline, and how do you avoid them?
Answers use simple, clear English.
Quick interview answer
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.
Detailed answer
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. 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
Open one as its own read / solve / listen card