The aggregation pipeline processes documents through ordered stages like $match, $group, $project and $sort. Each stage transforms the stream and passes results to the next one, enabling complex analytics similar to SQL.
[
{ "$match": { "status": "paid" } },
{ "$group": { "_id": "$customerId", "total": { "$sum": "$amount" } } },
{ "$sort": { "total": -1 } }
]