$group
The $group aggregation stage in Amazon DocumentDB allows you to group documents by a specified expression and perform various accumulative operations on the grouped data. This can be useful for tasks such as calculating totals, averages, or other statistics based on the grouped data.
Parameters
-
_id: Specifies the expression by which the input documents should be grouped. This can be a field name, a computed expression, or a combination of both. -
accumulator expressions: (optional) One or more accumulator expressions that should be applied to the grouped data. These expressions use the accumulator operators mentioned above.
Example (MongoDB Shell)
The following example groups customers by their city and calculates the total order amount for each city.
Create sample documents
db.customers.insertMany([ { name: "John Doe", city: "New York", orders: [{ amount: 100 }, { amount: 200 }] }, { name: "Jane Smith", city: "Los Angeles", orders: [{ amount: 150 }, { amount: 300 }] }, { name: "Bob Johnson", city: "New York", orders: [{ amount: 75 }, { amount: 125 }] }, { name: "Samantha Lee", city: "Chicago", orders: [{ amount: 50 }, { amount: 100 }] } ]);
Query example
db.customers.aggregate([ { $group: { _id: "$city", totalOrders: { $sum: { $sum: "$orders.amount" } } } } ]);
Output
[
{ _id: 'Chicago', totalOrders: 150 },
{ _id: 'Los Angeles', totalOrders: 450 },
{ _id: 'New York', totalOrders: 500 }
]
Code examples
To view a code example for using the $group command, choose the tab for the language that you want to use: