The aggregation pipeline is MongoDB most powerful feature. Learn to group, join, filter, and transform data like a pro.

Abdur Razzak
Full-Stack Web Developer
The aggregation pipeline is a series of stages that transform documents as they pass through. Each stage receives the output of the previous stage. Common stages include $match, $group, $sort, $project, $lookup, and $unwind.
Always place $match early in the pipeline to filter documents before expensive operations. $group accumulates values from multiple documents — like summing order totals by customer or counting posts by category.
$lookup performs a left outer join between two collections. Specify the from collection, the local and foreign field names, and an alias for the joined data. The result is an array field added to each document.
Create indexes on fields used in $match and $sort stages. Use $project to drop unused fields early and reduce the data carried through subsequent stages. For very large result sets, consider using allowDiskUse to enable on-disk sorting.