Pipeline Practice

Tune a lean category report

Performance work starts with pipeline shape. Reduce the stream before expensive stages, remove unused fields before a join, preserve the correct grain, and avoid extra stages that do not change the final contract.

For completed web orders, report revenue by region and product category. `$match` must run first, `$project` must trim documents before `$lookup`, and the complete pipeline must use no more than nine stages.

Expected result: A descending regional category report with `region`, `category`, and `revenue`, produced by a deliberately lean pipeline.

Example pipeline syntax

[
  { $lookup: { from: "products", localField: "items.productId", foreignField: "_id", as: "product" } }
]