The aggregation pipeline processes documents through stages—like a composable ETL inside the database.
First pipeline
db.orders.aggregate([
{ $match: { status: 'shipped' } },
{ $group: { _id: '$customerId', totalSpent: { $sum: '$total' } } },
{ $sort: { totalSpent: -1 } },
{ $limit: 10 }
])Practice: Run aggregation pipelines in mongosh.
Stage order matters
Put $match early to reduce documents—indexes can help $match like find().
Important interview questions and answers
- Q: aggregate vs find?
A: aggregate for transforms, grouping, joins; find for simple filters. - Q: $group _id?
A: Defines grouping key—null groups everything into one bucket.
Self-check
- Name first two stages in example.
- Why $match early?
Tip: Put $match first to shrink working set before $group.
Interview prep
- Pipeline?
- Ordered stages transforming documents.
- $match early?
- Reduces documents before expensive stages.