AWS Security ChangesHomeSearch

AWS documentdb documentation change

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

File: documentdb/latest/developerguide/replaceWith.md

Summary

Added new documentation page for the $replaceWith aggregation stage in Amazon DocumentDB, including syntax, parameters, examples in MongoDB Shell, Node.js, and Python, with connection strings and usage details

Security assessment

This change adds documentation for a new aggregation operator ($replaceWith) in DocumentDB. While the code examples include TLS configuration (tls=true, tlsCAFile=global-bundle.pem) which is security-related, there is no evidence this addresses a specific security vulnerability or incident. The change appears to be routine feature documentation.

Diff

diff --git a/documentdb/latest/developerguide/replaceWith.md b/documentdb/latest/developerguide/replaceWith.md
index 8b1378917..bcde52807 100644
--- a//documentdb/latest/developerguide/replaceWith.md
+++ b//documentdb/latest/developerguide/replaceWith.md
@@ -0,0 +1 @@
+[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#replaceWith "Open PDF")
@@ -1,0 +3,147 @@
+[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html)
+
+Example (MongoDB Shell)Code examples
+
+# $replaceWith
+
+New from version 8.0
+
+Not supported by Elastic cluster.
+
+The `$replaceWith` aggregation stage in Amazon DocumentDB is used to replace the input document with a new document. All existing fields on the input document, including the _id field, are replaced by the new document. `$replaceWith` is commonly used to flatten documents, or promote an embedded document to the top level.
+
+**Parameters**
+
+  * `<replacement>` (required): The new document that will replace the existing document.
+
+
+
+
+## Example (MongoDB Shell)
+
+The following example demonstrates how to use the `$replaceWith` operator to replace an existing document in an Amazon DocumentDB collection.
+
+**Create sample documents**
+    
+    
+    db.restaurants.insertMany([
+      {
+        "restaurantId": "REST-0Y9GL0",
+        "name": "Biryani Adda",
+        "cuisine": "Indian",
+        "ratings": [ 3, 4, 3, 2, 2, 4, 1, 5, 5, 5 ]
+      },
+      {
+        "restaurantId": "REST-8L2PX9",
+        "name": "The Burger Spot",
+        "cuisine": "American",
+        "ratings": [ 2, 3, 4, 5, 3, 1, 1, 2, 4 ]
+      }
+    ]);
+
+**Query example**
+    
+    
+    db.restaurants.aggregate([
+      { $replaceWith: {
+          name: "$name",
+          cuisine: "$cuisine",
+          rating: { $avg: "$ratings" }
+        }
+      }
+    ]);
+
+**Output**
+    
+    
+    [
+      {
+        name: 'Biryani Adda',
+        cuisine: 'Indian',
+        rating: 3.4
+      },
+      {
+        name: 'The Burger Spot',
+        cuisine: 'American',
+        rating: 2.7777777777777777
+      }
+    ]
+
+## Code examples
+
+To view a code example for using the `$replaceWith` command, choose the tab for the language that you want to use:
+
+Node.js
+    
+    
+    
+    const { MongoClient } = require('mongodb');
+    
+    async function replaceDoc() {
+      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('restaurants');
+    
+      const result = await collection.aggregate([
+        { $replaceWith: {
+            name: "description_index",
+            cuisine: 2,
+            rating: { $avg: "$ratings" }
+          }
+        }
+      ]).toArray();
+    
+      console.log(result);
+      client.close();
+    }
+    
+    replaceDoc();
+
+Python
+    
+    
+    
+    from pymongo import MongoClient
+    
+    
+    def replace_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.restaurants
+    
+        result = list(collection.aggregate([
+            {
+                '$replaceWith': {
+                    'name': "$name",
+                    'cuisine': "$cuisine",
+                    'rating': { '$avg': "$ratings"}
+                }
+            }
+        ]))
+    
+        print(result)
+        client.close()
+    
+    replace_document()
+
+![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)
+
+$replaceRoot
+
+$reverseArray
+
+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.