Aggregation Framework

Aggregation operations process data records and return computed results. In MongoDB, you use the Aggregation Pipeline to transform and filter data through multiple stages.

The Pipeline Concept

Imagine a factory assembly line. Documents go into the start of the pipeline and are modified or filtered at each stage until you get the final result.

db.orders.aggregate([
  // Stage 1: Filter documents
  { $match: { status: "A" } },
  
  // Stage 2: Group by product and calculate total quantity
  { $group: { _id: "$item", totalQty: { $sum: "$qty" } } },
  
  // Stage 3: Sort by total quantity descending
  { $sort: { totalQty: -1 } }
])

Common Pipeline Stages

StageDescription
$matchFilters documents (similar to `find()`).
$groupGroups documents by a specific key and performs accumulations (sum, avg, etc.).
$projectReshapes documents (renames fields, calculates new fields, removes fields).
$sortSorts documents.
$limit / $skipLimits or skips a specified number of documents.
$unwindDeconstructs an array field from the input documents to output a document for each element.
$lookupPerforms a left outer join to another collection in the same database.

Complex Example: $lookup

The `$lookup` stage allows you to join two collections together.

db.orders.aggregate([
  {
    $lookup: {
      from: "inventory",       // Join with collection "inventory"
      localField: "item",      // field from orders
      foreignField: "sku",     // field from inventory
      as: "inventory_docs"     // output array field
    }
  }
])
Pro Tip: Use `$match` as early as possible in the pipeline to reduce the number of documents being processed in subsequent stages.