AWS documentdb documentation change
Summary
Added new documentation page for the $avg aggregation operator in Amazon DocumentDB, including parameters, MongoDB shell example, and code examples in Node.js and Python
Security assessment
This change adds standard documentation for a database aggregation operator ($avg) with example usage. The code examples include TLS connection parameters (tls=true, tlsCAFile=global-bundle.pem) which are standard secure connection practices, but this is not introducing new security documentation or addressing a specific security vulnerability. The change appears to be routine feature documentation.
Diff
diff --git a/documentdb/latest/developerguide/avg.md b/documentdb/latest/developerguide/avg.md index 8b1378917..19c9c8ca3 100644 --- a//documentdb/latest/developerguide/avg.md +++ b//documentdb/latest/developerguide/avg.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#avg "Open PDF") @@ -1,0 +3,115 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $avg + +The `$avg` aggregation operator in Amazon DocumentDB calculates the average value of the specified expression across the documents that are input to the stage. This operator is useful for computing the average of a numeric field or expression across a set of documents. + +**Parameters** + + * `expression`: The expression to use to calculate the average. This can be a field path (e.g. `"$field"`) or an expression (e.g. `{ $multiply: ["$field1", "$field2"] }`). + + + + +## Example (MongoDB Shell) + +The following example demonstrates how to use the `$avg` operator to calculate the average score across a set of student documents. + +**Create sample documents** + + + db.students.insertMany([ + { name: "John", score: 85 }, + { name: "Jane", score: 92 }, + { name: "Bob", score: 78 }, + { name: "Alice", score: 90 } + ]); + +**Query example** + + + db.students.aggregate([ + { $group: { + _id: null, + avgScore: { $avg: "$score" } + }} + ]); + +**Output** + + + [ + { + "_id": null, + "avgScore": 86.25 + } + ] + +## Code examples + +To view a code example for using the `$avg` command, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + async function calculateAvgScore() { + const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); + const db = client.db('test'); + const result = await db.collection('students').aggregate([ + { $group: { + _id: null, + avgScore: { $avg: '$score' } + }} + ]).toArray(); + console.log(result); + await client.close(); + } + + calculateAvgScore(); + +Python + + + + from pymongo import MongoClient + + def calculate_avg_score(): + client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') + db = client.test + result = list(db.students.aggregate([ + { '$group': { + '_id': None, + 'avgScore': { '$avg': '$score' } + }} + ])) + print(result) + client.close() + + calculate_avg_score() + + **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) + +$arrayToObject + +$bucket + +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.