AWS Security ChangesHomeSearch

AWS documentdb documentation change

Service: documentdb · 2026-03-31 · Documentation low

File: documentdb/latest/developerguide/slice.md

Summary

Added new documentation page for the $slice aggregation operator in Amazon DocumentDB, including operator description, parameters, MongoDB Shell example, and code examples in Node.js and Python

Security assessment

This is routine documentation addition for a database operator functionality. The change shows standard usage examples of the $slice operator for array manipulation. The code examples include TLS connection parameters (tls=true, tlsCAFile) which are standard security practices for DocumentDB connections, but this is not new security documentation - it's just showing standard secure connection patterns. No evidence of addressing a specific security vulnerability or incident.

Diff

diff --git a/documentdb/latest/developerguide/slice.md b/documentdb/latest/developerguide/slice.md
index 8b1378917..846a0d353 100644
--- a//documentdb/latest/developerguide/slice.md
+++ b//documentdb/latest/developerguide/slice.md
@@ -0,0 +1 @@
+[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#slice "Open PDF")
@@ -1,0 +3,116 @@
+[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html)
+
+Example (MongoDB Shell)Code examples
+
+# $slice
+
+The `$slice` aggregation operator enables you to return a subset of an array by either traversing the array from the beginning or the end of the array. This is used to display a limited number of items from an array field, such as the top or bottom N items.
+
+**Parameters**
+
+  * `array`: The array field to be sliced.
+
+  * `n`: An integer that specifies the number of elements to return. A positive value starts from the beginning of the array, while a negative value starts from the end of the array.
+
+
+
+
+## Example (MongoDB Shell)
+
+The following example demonstrates how to use `$slice` to return the first two favorite sweets for each chef.
+
+**Create sample documents**
+    
+    
+    db.sweets.insertMany([
+      { "_id" : 1, "name" : "Alvin", "favorites": [ "chocolate", "cake", "toffee", "beignets" ] },
+      { "_id" : 2, "name" : "Tom", "favorites": [ "donuts", "pudding", "pie" ] },
+      { "_id" : 3, "name" : "Jessica", "favorites": [ "fudge", "smores", "pudding", "cupcakes" ] },
+      { "_id" : 4, "name" : "Rachel", "favorites": [ "ice cream" ] }
+    ]);
+
+**Query example**
+    
+    
+    db.sweets.aggregate([
+      { $project: { _id: 0, name: 1, topTwoFavorites: { $slice: [ "$favorites", 2 ] } } }
+    ]);
+
+**Output**
+    
+    
+    [
+      { name: 'Alvin', topTwoFavorites: [ 'chocolate', 'cake' ] },
+      { name: 'Tom', topTwoFavorites: [ 'donuts', 'pudding' ] },
+      { name: 'Jessica', topTwoFavorites: [ 'fudge', 'smores' ] },
+      { name: 'Rachel', topTwoFavorites: [ 'ice cream' ] }
+    ]
+
+In this example, the `$slice` operator is used to extract the first two elements from the `favorites` array for each document.
+
+## Code examples
+
+To view a code example for using the `$slice` command, choose the tab for the language that you want to use:
+
+Node.js
+    
+    
+    
+    const { MongoClient } = require('mongodb');
+    
+    async function example() {
+      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('sweets');
+    
+      const result = await collection.aggregate([
+        { $project: { name: 1, topTwoFavorites: { $slice: ['$favorites', 2] } } }
+      ]).toArray();
+    
+      console.log(result);
+      await client.close();
+    }
+    
+    example();
+
+Python
+    
+    
+    
+    from pymongo import MongoClient
+    
+    def example():
+        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['sweets']
+    
+        result = list(collection.aggregate([
+            { '$project': { 'name': 1, 'topTwoFavorites': { '$slice': ['$favorites', 2] } } }
+        ]))
+    
+        print(result)
+        client.close()
+    
+    example()
+
+![Warning](https://d1ge0kk1l5kms0.cloudfront.net/images/G/01/webservices/console/warning.png) **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)
+
+$skip
+
+$size
+
+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.