The aggregation pipeline passes documents through stages, like an assembly line:
$match: filter (like WHERE).$group: group and calculate (like GROUP BY), with$sum,$avg,$max…$sort,$limit,$project,$lookup(join another collection).
Group, filter and transform data in stages.
✍️ 2 exercises in this lessonThe aggregation pipeline passes documents through stages, like an assembly line:
$match: filter (like WHERE).$group: group and calculate (like GROUP BY), with $sum, $avg, $max…$sort, $limit, $project, $lookup (join another collection).Mastering MongoDB Aggregation Pipeline helps you write clean and reliable MongoDB code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
db.students.aggregate([
{ $match: { marks: { $gte: 60 } } },
{ $group: { _id: "$city", students: { $sum: 1 }, avgMarks: { $avg: "$marks" } } },
{ $sort: { avgMarks: -1 } }
])
[
{ _id: 'Karachi', students: 1, avgMarks: 94 },
{ _id: 'Lahore', students: 1, avgMarks: 81 },
{ _id: 'Rawalpindi', students: 1, avgMarks: 72 }
]
Check what you learned in “MongoDB Aggregation Pipeline”.
The stage that filters documents (like WHERE).
db.orders.aggregate([ { : { status: "paid" } } ])
Which stage groups documents and calculates totals?
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services