Aggregation Pipeline Operators
The aggregation pipeline processes documents through a sequence of stages — $match, $group, $project, $sort, $lookup — like a data processing pipeline.
db.orders.aggregate([
{ $match: { status: 'shipped' } },
{ $group: { _id: '$customerId', total: { $sum: '$amount' } } },
{ $sort: { total: -1 } },
{ $limit: 5 },
]);
- $match — filter documents (like a WHERE clause)
- $group — aggregate values per key (like GROUP BY)
- $project — reshape documents, include/exclude/compute fields
- $lookup — join data from another collection