AWS documentdb documentation change
Summary
Added new documentation page for the $addFields aggregation pipeline stage in Amazon DocumentDB, including description, parameters, MongoDB Shell example, and code examples in Node.js and Python
Security assessment
This change adds routine documentation for a database aggregation feature ($addFields) without any security context. The content includes standard code examples for connecting to DocumentDB with TLS enabled (as shown in the connection strings with tls=true and tlsCAFile parameters), but this is standard security practice for database connections, not a response to a specific security issue. The change appears to be routine feature documentation addition.
Diff
diff --git a/documentdb/latest/developerguide/addFields.md b/documentdb/latest/developerguide/addFields.md index 8b1378917..929f60cc8 100644 --- a//documentdb/latest/developerguide/addFields.md +++ b//documentdb/latest/developerguide/addFields.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#addFields "Open PDF") @@ -1,0 +3,154 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $addFields + +The `$addFields` stage in the Amazon DocumentDB aggregation pipeline allows you to add new computed fields to documents. This can be useful for adding derived or transformed data to the documents. + +**Parameters** + + * `newField`: The name of the new field to add. + + * `expression`: An expression that resolves to the value of the new field. + + + + +## Example (MongoDB Shell) + +The following example demonstrates how to use `$addFields` to add a new field `TotalInventory` that calculates the total inventory based on the `Inventory.OnHand` and `Inventory.OrderQnty` fields. + +**Create sample documents** + + + db.example.insertMany([ + { + "Item": "Spray Paint", + "Colors": ["Black", "Red", "Green", "Blue"], + "Inventory": { + "OnHand": 47, + "MinOnHand": 50, + "OrderQnty": 36 + }, + "UnitPrice": 3.99 + }, + { + "Item": "Ruler", + "Colors": ["Red", "Green", "Blue", "Clear", "Yellow"], + "Inventory": { + "OnHand": 47, + "MinOnHand": 40 + }, + "UnitPrice": 0.89 + } + ]); + +**Query example** + + + db.example.aggregate([ + { + $addFields: { + TotalInventory: { $add: ["$Inventory.OnHand", "$Inventory.OrderQnty"] } + } + } + ]) + +**Output** + + + [ + { + "_id" : ObjectId("5bedafbcf65ff161707de24f"), + "Item" : "Ruler", + "Colors" : [ "Red", "Green", "Blue", "Clear", "Yellow" ], + "Inventory" : { + "OnHand" : 47, + "MinOnHand" : 40 + }, + "UnitPrice" : 0.89, + "TotalInventory" : 47 + }, + { + "_id" : ObjectId("5bedafbcf65ff161707de250"), + "Item" : "Spray Paint", + "Colors" : [ "Black", "Red", "Green", "Blue" ], + "Inventory" : { + "OnHand" : 47, + "MinOnHand" : 50, + "OrderQnty" : 36 + }, + "UnitPrice" : 3.99, + "TotalInventory" : 83 + } + ] + +## Code examples + +To view a code example for using the `$addFields` command, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + 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('example'); + + const result = await collection.aggregate([ + { + $addFields: { + TotalInventory: { $add: ['$Inventory.OnHand', '$Inventory.OrderQnty'] } + } + } + ]).toArray(); + + console.log(result); + + await client.close(); + +Python + + + + from pymongo import MongoClient + + 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['example'] + + result = list(collection.aggregate([ + { + '$addFields': { + 'TotalInventory': { '$add': ['$Inventory.OnHand', '$Inventory.OrderQnty'] } + } + } + ])) + + print(result) + client.close() + + **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) + +$addToSet + +$allElementsTrue + +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.