Aggregations process data and return computed results. They are used for deep analytics and data summarization. They function exactly like the GROUP BY clause in SQL.
MongoDB uses a pipeline concept. Documents pass through multiple stages in an array. Each stage transforms the data before passing it down.
The $match stage filters the documents first. It works exactly like a standard find() query. Always put $match early in the pipeline to improve speed.
The $group stage groups documents by a specific key. You must define an _id for the grouping. You can compute sums, averages, or counts here.
After grouping, the data can be ordered. The $sort stage arranges the final output cleanly. You can sort by the new computed fields.
// Find total sales per region for active accounts
db.sales.aggregate([
{ $match: { status: "active" } },
{ $group: {
_id: "$region",
totalSales: { $sum: "$amount" }
}},
{ $sort: { totalSales: -1 } }
]);
The $project stage formats the final output document. You can rename fields, calculate new ones, or hide them. It shapes the final JSON precisely for your frontend.
MongoDB is a NoSQL database without foreign keys. However, $lookup performs a left outer join. It merges data from another collection seamlessly.
Aggregations can be resource-intensive on big collections. Ensure your $match and $sort stages use indexes. Test heavy pipelines on background secondary nodes.
The aggregation framework handles complex data grouping. It operates in sequential stages through a pipeline. Use $group, $match, and $lookup to analyze data.
Which aggregation stage works similarly to a standard find() query filter?