Basic Pipeline Stages in MongoDB
From the MongoDB cheat sheet ยท Aggregation Pipeline ยท verified Jul 2026
Basic Pipeline Stages
Common stages for data transformation
javascript
// Basic aggregation pipeline
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: {
_id: "$customerId",
totalSpent: { $sum: "$total" },
orderCount: { $sum: 1 }
}},
{ $sort: { totalSpent: -1 } },
{ $limit: 10 }
])
// Project (reshape) documents
db.users.aggregate([
{ $project: {
name: 1,
email: 1,
fullName: { $concat: ["$firstName", " ", "$lastName"] },
age: { $subtract: [
{ $year: new Date() },
{ $year: "$birthDate" }
]}
}}
])๐ข Essential - Powerful data processing
๐ก Pipeline stages process sequentially
โก $match early to reduce documents
๐ $group _id can be complex expression
๐ Related: $facet for multiple pipelines
aggregationpipeline