AWS documentdb documentation change
Summary
Added comprehensive documentation for the $group aggregation stage in Amazon DocumentDB, including parameters, MongoDB Shell example, and code examples in Node.js and Python with connection strings.
Security assessment
This change adds standard documentation for a database aggregation operator ($group) with example code. The examples include connection strings with TLS parameters (tls=true, tlsCAFile=global-bundle.pem) which are standard security practices for DocumentDB connections, but this is routine documentation of existing security features rather than addressing a specific security issue. There is no evidence of vulnerability fixes, security patches, or incident response documentation.
Diff
diff --git a/documentdb/latest/developerguide/group.md b/documentdb/latest/developerguide/group.md index 8b1378917..31127ba71 100644 --- a//documentdb/latest/developerguide/group.md +++ b//documentdb/latest/developerguide/group.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#group "Open PDF") @@ -1,0 +3,138 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $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() + + **Javascript is disabled or is unavailable in your browser.** + +To use the Amazon Web Services Documentation, Javascript must be enabled. Please refer to your browser's Help pages for instructions. + +[Document Conventions](/general/latest/gr/docconventions.html) + +$geoNear + +$gt + +Did this page help you? - Yes + +Thanks for letting us know we're doing a good job! + +If you've got a moment, please tell us what we did right so we can do more of it. + +Did this page help you? - No + +Thanks for letting us know this page needs work. We're sorry we let you down. + +If you've got a moment, please tell us how we can make the documentation better.