MongoDB — Aggregation Pipeline Introduction
Learn how MongoDB Aggregation Pipeline transforms documents through multi-stage data processing pipes.
1What is the Aggregation Pipeline?
The Aggregation Pipeline is a framework for data aggregation modeled on data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into aggregated results.
Basic Pipeline Example
db.orders.aggregate([
// Stage 1: Filter active orders
{ $match: { status: "completed" } },
// Stage 2: Group by customerId and sum total spend
{ $group: { _id: "$customerId", totalSpent: { $sum: "$total" } } },
// Stage 3: Sort by totalSpent descending
{ $sort: { totalSpent: -1 } }
]);