AWS Security ChangesHomeSearch

AWS documentdb documentation change

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

File: documentdb/latest/developerguide/replaceOne.md

Summary

Added new documentation page for the $replaceOne operator in Amazon DocumentDB, including syntax, parameters, examples in MongoDB Shell, Node.js, and Python, and usage in aggregation pipelines.

Security assessment

This change adds documentation for a new string expression operator ($replaceOne) in Amazon DocumentDB. The documentation includes standard usage examples and code samples. There is no mention of security vulnerabilities, patches, or security-related fixes. The change appears to be routine feature documentation for a new operator introduced in version 5.0. The code examples show standard database operations without any security warnings or security-specific guidance.

Diff

diff --git a/documentdb/latest/developerguide/replaceOne.md b/documentdb/latest/developerguide/replaceOne.md
index 8b1378917..85514b351 100644
--- a//documentdb/latest/developerguide/replaceOne.md
+++ b//documentdb/latest/developerguide/replaceOne.md
@@ -0,0 +1 @@
+[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#replaceOne "Open PDF")
@@ -1,0 +3,174 @@
+[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html)
+
+Example (MongoDB Shell)Code examples
+
+# $replaceOne
+
+Introduced in 5.0
+
+The `$replaceOne` operator in Amazon DocumentDB is a string expression operator used within aggregation pipelines to replace the first occurrence of a specified substring within a string with a replacement string. This operator is case-sensitive and only replaces the first match found.
+
+**Parameters**
+
+  * `input`: The string (field) on which to perform the find.
+
+  * `find`: The string to search for within the input.
+
+  * `replacement`: The string to replace the first occurrence of the find in the input(field).
+
+
+
+
+## Example (MongoDB Shell)
+
+The following example demonstrates how to use the `$replaceOne` operator within an aggregation pipeline to replace substrings in product names.
+
+**Create sample documents**
+    
+    
+    db.products.insertMany([
+      { "_id":1, "productId": "PROD-0Y9GL0", "name": "Gordon's Extra Creamy Milk Chocolate - Pack of 4", "category": "Confectionery", "price": 24.99 },
+      { "_id":2, "productId": "PROD-Y2E9H5", "name": "Nutrition Co. - Original Corn Flakes Cereal", "category": "Breakfast Cereals", "price": 8.50 },
+      { "_id":3, "productId": "PROD-Z3F8K2", "name": "Gordon's Dark Chocolate (90% Cocoa) Pack - Pack of 4", "category": "Confectionery", "price": 28.99 }
+    ]);
+    
+
+**Aggregation example**
+    
+    
+    db.products.aggregate([
+      {
+        $addFields: {
+          standardizedName: {
+            $replaceOne: {
+              input: "$name",
+              find: "Pack",
+              replacement: "Package"
+            }
+          }
+        }
+      }
+    ]);
+
+**Output**
+
+The output shows that only the first occurrence of "Pack" in each product name was replaced with "Package".
+    
+    
+    [
+      {
+        _id: 1,
+        productId: 'PROD-0Y9GL0',
+        name: "Gordon's Extra Creamy Milk Chocolate - Pack of 4",
+        category: 'Confectionery',
+        price: 24.99,
+        standardizedName: "Gordon's Extra Creamy Milk Chocolate - Package of 4"
+      },
+      {
+        _id: 2,
+        productId: 'PROD-Y2E9H5',
+        name: 'Nutrition Co. - Original Corn Flakes Cereal',
+        category: 'Breakfast Cereals',
+        price: 8.5,
+        standardizedName: 'Nutrition Co. - Original Corn Flakes Cereal'
+      },
+      {
+        _id: 3,
+        productId: 'PROD-Z3F8K2',
+        name: "Gordon's Dark Chocolate (90% Cocoa) Pack - Pack of 4",
+        category: 'Confectionery',
+        price: 28.99,
+        standardizedName: "Gordon's Dark Chocolate (90% Cocoa) Package - Pack of 4"
+      }
+    
+
+## Code examples
+
+To view a code example for using the `$replaceOne` command, choose the tab for the language that you want to use:
+
+Node.js
+    
+    
+    
+    const { MongoClient } = require('mongodb');
+    
+    async function replaceOne() {
+      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('products');
+    
+      const pipeline = [
+        {
+          $addFields: {
+            standardizedName: {
+              $replaceOne: {
+                input: '$name',
+                find: 'Pack',
+                replacement: 'Package'
+              }
+            }
+          }
+        }
+      ];
+    
+      const result = await collection.aggregate(pipeline).toArray();
+      console.log(result);
+    
+      await client.close();
+    }
+    
+    replaceOne();
+
+Python
+    
+    
+    
+    from pymongo import MongoClient
+    
+    def replaceOne():
+        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['products']
+    
+        pipeline = [
+            {
+                '$addFields': {
+                    'standardizedName': {
+                        '$replaceOne': {
+                            'input': '$name',
+                            'find': 'Pack',
+                            'replacement': 'Package'
+                        }
+                    }
+                }
+            }
+        ]
+    
+        result = list(collection.aggregate(pipeline))
+        print(result)
+    
+        client.close()
+    
+    replaceOne()
+
+![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)
+
+$replaceAll
+
+$replaceRoot
+
+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.