$push
The $push aggregation operator returns an array of all values from a specified expression for each group. It is typically used within the $group stage to accumulate values into an array.
Parameters
-
expression: The expression to evaluate for each document in the group.
Example (MongoDB Shell)
The following example demonstrates using the $push operator to collect all product names for each category.
Create sample documents
db.sales.insertMany([ { _id: 1, category: "Electronics", product: "Laptop", amount: 1200 }, { _id: 2, category: "Electronics", product: "Mouse", amount: 25 }, { _id: 3, category: "Furniture", product: "Desk", amount: 350 }, { _id: 4, category: "Furniture", product: "Chair", amount: 150 }, { _id: 5, category: "Electronics", product: "Keyboard", amount: 75 } ]);
Query example
db.sales.aggregate([ { $group: { _id: "$category", products: { $push: "$product" } } } ]);
Output
[
{ _id: 'Furniture', products: [ 'Desk', 'Chair' ] },
{ _id: 'Electronics', products: [ 'Laptop', 'Mouse', 'Keyboard' ] }
]
Code examples
To view a code example for using the $push aggregation operator, choose the tab for the language that you want to use: