AWS Security ChangesHomeSearch

AWS documentdb documentation change

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

File: documentdb/latest/developerguide/sample.md

Summary

Added new documentation page for the $sample aggregation stage in Amazon DocumentDB with examples in MongoDB Shell, Node.js, and Python

Security assessment

This change adds standard documentation for a database aggregation operator ($sample) with code examples. The examples include TLS connection parameters (tls=true, tlsCAFile=global-bundle.pem) which are standard security practices for encrypted connections, but this is not new security documentation - it's just standard secure connection examples. There is no evidence this addresses a specific security vulnerability or incident.

Diff

diff --git a/documentdb/latest/developerguide/sample.md b/documentdb/latest/developerguide/sample.md
index 8b1378917..4ae3d028b 100644
--- a//documentdb/latest/developerguide/sample.md
+++ b//documentdb/latest/developerguide/sample.md
@@ -0,0 +1 @@
+[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#sample "Open PDF")
@@ -1,0 +3,116 @@
+[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html)
+
+Example (MongoDB Shell)Code examples
+
+# $sample
+
+The `$sample` aggregation stage in Amazon DocumentDB is used to randomly select a specified number of documents from a collection. This is useful for tasks such as data analysis, testing, and generating samples for further processing.
+
+**Parameters**
+
+  * `size`: The number of documents to randomly select.
+
+
+
+
+## Example (MongoDB Shell)
+
+The following example demonstrates how to use the `$sample` stage to randomly select two documents from the `temp` collection.
+
+**Create sample documents**
+    
+    
+    db.temp.insertMany([
+      { "_id": 1, "temperature": 97.1, "humidity": 0.60, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 2, "temperature": 98.2, "humidity": 0.59, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 3, "temperature": 96.8, "humidity": 0.61, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 4, "temperature": 97.9, "humidity": 0.61, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 5, "temperature": 97.5, "humidity": 0.60, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 6, "temperature": 98.0, "humidity": 0.59, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 7, "temperature": 97.2, "humidity": 0.60, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 8, "temperature": 98.1, "humidity": 0.59, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 9, "temperature": 96.9, "humidity": 0.62, "timestamp": ISODate("2019-03-21T21:17:22.425Z") },
+      { "_id": 10, "temperature": 97.7, "humidity": 0.60, "timestamp": ISODate("2019-03-21T21:17:22.425Z") }
+    ]);
+
+**Query example**
+    
+    
+    db.temp.aggregate([
+       { $sample: { size: 2 } }
+    ])
+
+**Output**
+    
+    
+    { "_id" : 4, "temperature" : 97.9, "humidity" : 0.61, "timestamp" : ISODate("2019-03-21T21:17:22.425Z") }
+    { "_id" : 9, "temperature" : 96.9, "humidity" : 0.62, "timestamp" : ISODate("2019-03-21T21:17:22.425Z") }
+
+As the results show, 2 of the 10 documents were randomly sampled. You can now use these documents to determine an average or to perform min/max calculations.
+
+## Code examples
+
+To view a code example for using the `$sample` command, choose the tab for the language that you want to use:
+
+Node.js
+    
+    
+    
+    const { MongoClient } = require('mongodb');
+    
+    async function sampleDocuments() {
+      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('temp');
+    
+      const result = await collection.aggregate([
+        { $sample: { size: 2 } }
+      ]).toArray();
+    
+      console.log(result);
+      await client.close();
+    }
+    
+    sampleDocuments();
+
+Python
+    
+    
+    
+    from pymongo import MongoClient
+    
+    def sample_documents():
+        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['temp']
+    
+        result = list(collection.aggregate([
+            { '$sample': { 'size': 2 } }
+        ]))
+    
+        print(result)
+        client.close()
+    
+    sample_documents()
+
+![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)
+
+$rtrim
+
+$search
+
+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.