Problem Statement
Describe the most commonly used aggregation pipeline stages and their purposes.
Explanation
The most commonly used aggregation stages are dollar match, dollar group, dollar project, dollar sort, dollar limit, dollar skip, dollar unwind, and dollar lookup.
Dollar match filters documents using query operators, similar to find. Place it early in the pipeline to reduce data volume. Dollar group groups documents by a specified field and performs accumulator operations like dollar sum, dollar avg, dollar min, dollar max, dollar push, and dollar first.
Dollar project reshapes documents by including, excluding, or computing fields. Use it to create new fields with expressions or to reduce document size. Dollar sort orders documents by specified fields in ascending or descending order.
Dollar limit restricts the number of documents passed to the next stage, while dollar skip skips a specified number of documents. These are useful for pagination. Dollar unwind deconstructs array fields, creating one document per array element.
Dollar lookup performs left outer joins with other collections, similar to SQL joins. It adds an array field containing matching documents from the joined collection. Use these stages in combination to perform complex data analysis and transformations.
Code Solution
SolutionRead Only
// Complete pipeline example
db.sales.aggregate([
{ $match: { date: { $gte: ISODate("2024-01-01") } } },
{ $unwind: "$items" },
{ $group: { _id: "$items.category", revenue: { $sum: "$items.price" } } },
{ $sort: { revenue: -1 } },
{ $limit: 5 },
{ $project: { category: "$_id", revenue: 1, _id: 0 } }
])