AWS documentdb documentation change
Summary
Added new documentation page for the $slice update operator in Amazon DocumentDB, including examples in MongoDB Shell, Node.js, and Python with connection strings
Security assessment
This change adds documentation for a MongoDB operator ($slice) used for array manipulation. The code examples include standard TLS connection parameters (tls=true, tlsCAFile) which are normal secure connection practices for DocumentDB, 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/slice-update.md b/documentdb/latest/developerguide/slice-update.md index 8b1378917..ff77fb706 100644 --- a//documentdb/latest/developerguide/slice-update.md +++ b//documentdb/latest/developerguide/slice-update.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#slice-update "Open PDF") @@ -1,0 +3,142 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $slice + +The `$slice` update operator modifies an array by limiting its size. When used with the `$push` operator, it restricts the number of elements in an array, keeping only the specified number of most recent or oldest elements. + +**Parameters** + + * `field`: The array field to modify. + + * `count`: Maximum number of elements to keep. Positive values keep the first N elements, negative values keep the last N elements. + + + + +## Example (MongoDB Shell) + +The following example demonstrates how to use the `$slice` update operator with `$push` to maintain a fixed-size array of recent scores. + +**Create sample documents** + + + db.students.insertOne({ + _id: 1, + name: "Alice", + scores: [85, 90, 78] + }); + +**Query example** + + + db.students.updateOne( + { _id: 1 }, + { + $push: { + scores: { + $each: [92, 88], + $slice: -3 + } + } + } + ) + +**Output** + + + { + "_id" : 1, + "name" : "Alice", + "scores" : [ 78, 92, 88 ] + } + +In this example, the `$slice: -3` modifier keeps only the last three elements after pushing new values to the array. + +## Code examples + +To view a code example for using the `$slice` update operator, 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: { + scores: { + $each: [92, 88], + $slice: -3 + } + } + } + ); + + 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': { + 'scores': { + '$each': [92, 88], + '$slice': -3 + } + } + } + ) + + 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) + +$setOnInsert + +$sort + +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.