AWS documentdb documentation change
Summary
Added new documentation page for the $sort update modifier in Amazon DocumentDB, including syntax, parameters, MongoDB shell example, and code examples in Node.js and Python
Security assessment
This change adds documentation for a database operator ($sort) used with $push to order array elements. The code examples include TLS connection parameters (tls=true, tlsCAFile=global-bundle.pem) which are standard security practices for DocumentDB connections, but this is not new security documentation - it's standard connection security that was likely already documented elsewhere. There is no evidence this addresses a specific security vulnerability or incident.
Diff
diff --git a/documentdb/latest/developerguide/sort-update.md b/documentdb/latest/developerguide/sort-update.md index 8b1378917..8ca206965 100644 --- a//documentdb/latest/developerguide/sort-update.md +++ b//documentdb/latest/developerguide/sort-update.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#sort-update "Open PDF") @@ -1,0 +3,147 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $sort + +The `$sort` update modifier orders array elements when used with the `$push` operator. It arranges array elements in ascending or descending order based on specified field values or the elements themselves. + +**Parameters** + + * `field`: The array field to modify. + + * `order`: Use `1` for ascending order or `-1` for descending order. + + + + +## Example (MongoDB Shell) + +The following example demonstrates using the `$sort` modifier with `$push` to add new quiz scores and keep them sorted in descending order. + +**Create sample documents** + + + db.students.insertOne({ + _id: 1, + name: "Bob", + quizzes: [ + { score: 85, date: "2024-01-15" }, + { score: 92, date: "2024-02-10" } + ] + }); + +**Query example** + + + db.students.updateOne( + { _id: 1 }, + { + $push: { + quizzes: { + $each: [{ score: 78, date: "2024-03-05" }], + $sort: { score: -1 } + } + } + } + ) + +**Output** + + + { + "_id" : 1, + "name" : "Bob", + "quizzes" : [ + { "score" : 92, "date" : "2024-02-10" }, + { "score" : 85, "date" : "2024-01-15" }, + { "score" : 78, "date" : "2024-03-05" } + ] + } + +## Code examples + +To view a code example for using the `$sort` update modifier, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + async function updateDocument() { + 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 collection = db.collection('students'); + + await collection.updateOne( + { _id: 1 }, + { + $push: { + quizzes: { + $each: [{ score: 78, date: "2024-03-05" }], + $sort: { score: -1 } + } + } + } + ); + + const updatedDocument = await collection.findOne({ _id: 1 }); + console.log(updatedDocument); + + await client.close(); + } + + updateDocument(); + +Python + + + + from pymongo import MongoClient + + def update_document(): + 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.students + + collection.update_one( + {'_id': 1}, + { + '$push': { + 'quizzes': { + '$each': [{'score': 78, 'date': '2024-03-05'}], + '$sort': {'score': -1} + } + } + } + ) + + updated_document = collection.find_one({'_id': 1}) + print(updated_document) + + client.close() + + update_document() + + **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) + +$slice + +$unset + +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.