MongoDB Aggregations

Data Aggregation in MongoDB

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.

The Aggregation Pipeline

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

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

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.

The $sort Stage

After grouping, the data can be ordered. The $sort stage arranges the final output cleanly. You can sort by the new computed fields.

Aggregation Example:

// 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

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.

The $lookup Stage

MongoDB is a NoSQL database without foreign keys. However, $lookup performs a left outer join. It merges data from another collection seamlessly.

Performance Considerations

Aggregations can be resource-intensive on big collections. Ensure your $match and $sort stages use indexes. Test heavy pipelines on background secondary nodes.

Summary

The aggregation framework handles complex data grouping. It operates in sequential stages through a pipeline. Use $group, $match, and $lookup to analyze data.

Exercise

Which aggregation stage works similarly to a standard find() query filter?