$group - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function groupByCity() { const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; const client = new MongoClient(uri); try { await client.connect(); const db = client.db('test'); const result = await db.collection('customers').aggregate([ { $unwind: '$orders' }, { $group: { _id: '$city', totalOrders: { $sum: '$orders.amount' } } } ]).toArray(); console.log(result); } catch (err) { console.error('Error during aggregation:', err); } finally { await client.close(); } } groupByCity();
Python
from pymongo import MongoClient def group_by_city(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client.test result = list(db.customers.aggregate([ {'$unwind': '$orders'}, { '$group': { '_id': '$city', 'totalOrders': {'$sum': '$orders.amount'} } } ])) print(result) except Exception as e: print(f"Error during aggregation: {e}") finally: client.close() group_by_city()