QuickDevGuide

Home › MongoDB

Aggregation Pipeline — MongoDB

The aggregation pipeline transforms documents through stages, like a Unix pipe. Stage SQL analogue $match WHERE (put it first to use indexes) $project / $addFields / $set SELECT expressions $group GROUP BY with $sum, $avg, $min, $max, $push, $addToSet $sort, $limit, $skip ORDER BY, LIMIT, OFFSET $unwind one document per array element $lookup LEFT OUTER JOIN $facet several sub-pipelines on the same input $bucket histograms $merge / $out write results to a collection (materialised views) Field paths use "$field"; stages can use expression operators like $multiply, $cond, $dateToString.

Key Takeaways