The Aggregation Pipeline Framework

🍁 MongoDBLesson 10Intermediate

The aggregation pipeline provides a framework to perform data transformations, grouping operations, and data joins inside MongoDB.

1 Aggregation Pipeline Example
JavaScript — Aggregation Pipeline
db.orders.aggregate([
  # Stage 1: Filter active orders
  { $match: { status: "completed" } },
  # Stage 2: Group by category and compute total sales sum
  { $group: { _id: "$category", total_sales: { $sum: "$price" } } },
  # Stage 3: Sort categories by total sales volume
  { $sort: { total_sales: -1 } }
]);
2 Code Challenge
Challenge: Write an aggregation pipeline that joins a posts collection to a users collection using the $lookup operator to map author user names.