AWS documentdb documentation change
Summary
Added new documentation page for the $sum aggregation operator in Amazon DocumentDB, including syntax, parameters, MongoDB shell example, and code examples in Node.js and Python
Security assessment
This change adds standard documentation for a database aggregation operator ($sum) with code examples. The examples include standard connection strings with TLS parameters, but this is routine security best practice documentation, not addressing any specific security vulnerability or incident. The change appears to be routine feature documentation addition.
Diff
diff --git a/documentdb/latest/developerguide/sum.md b/documentdb/latest/developerguide/sum.md index 8b1378917..f4602800b 100644 --- a//documentdb/latest/developerguide/sum.md +++ b//documentdb/latest/developerguide/sum.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#sum "Open PDF") @@ -1,0 +3,139 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $sum + +The `$sum` operator in Amazon DocumentDB returns the sum of the specified expression for each document in a group. It is a group accumulator operator that is typically used in the $group stage of an aggregation pipeline to perform summation calculations. + +**Parameters** + + * `expression`: The numeric expression to sum. This can be a field path, an expression, or a constant. + + + + +## Example (MongoDB Shell) + +The following example demonstrates the use of the `$sum` operator to calculate the total sales for each product. + +**Create sample documents** + + + db.sales.insertMany([ + { product: "abc", price: 10, quantity: 2 }, + { product: "abc", price: 10, quantity: 3 }, + { product: "xyz", price: 20, quantity: 1 }, + { product: "xyz", price: 20, quantity: 5 } + ]); + +**Query example** + + + db.sales.aggregate([ + { $group: { + _id: "$product", + totalSales: { $sum: { $multiply: [ "$price", "$quantity" ] } } + }} + ]); + +**Output** + + + [ + { "_id": "abc", "totalSales": 50 }, + { "_id": "xyz", "totalSales": 120 } + ] + +## Code examples + +To view a code example for using the `$sum` command, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + async function example() { + 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 collection = db.collection('sales'); + + const result = await collection.aggregate([ + { $group: { + _id: "$product", + totalSales: { $sum: { $multiply: [ "$price", "$quantity" ] } } + }} + ]).toArray(); + + console.log(result); + + } catch (error) { + console.error('Error:', error); + } finally { + await client.close(); + } + } + + example(); + +Python + + + + from pymongo import MongoClient + from pprint import pprint + + def example(): + client = None + try: + client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') + + db = client.test + collection = db.sales + + result = list(collection.aggregate([ + { '$group': { + '_id': '$product', + 'totalSales': { '$sum': { '$multiply': [ '$price', '$quantity' ] } } + }} + ])) + + pprint(result) + + except Exception as e: + print(f"An error occurred: {e}") + + finally: + if client: + client.close() + + example() + + **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) + +$subtract + +$switch + +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.