MongoDB Aggregation Pipeline: A Practical Guide
The aggregation pipeline is MongoDB's answer to complex queries. Think of it as a Unix pipe for documents.
Key Insights
- Pipeline stages process documents sequentially — order matters for performance
- Put $match and $project early to reduce the dataset before expensive stages
- $lookup performs left outer joins between collections
Basic Pipeline
db.orders.aggregate([
{ $match: { status: "completed", date: { $gte: ISODate("2024-01-01") } } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" }, count: { $sum: 1 } } },
{ $sort: { total: -1 } },
{ $limit: 10 }
]);
$lookup for Joins
db.orders.aggregate([
{ $lookup: {
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customer"
}},
{ $unwind: "$customer" },
{ $project: { amount: 1, "customer.name": 1 } }
]);