AWS documentdb documentation change
Summary
Added new documentation page for the $arrayToObject operator in Amazon DocumentDB, including description, parameters, MongoDB shell example, and code examples in Node.js and Python
Security assessment
This change adds documentation for a new operator ($arrayToObject) in Amazon DocumentDB. The content describes the operator's functionality, provides usage examples, and includes code samples. There is no mention of security vulnerabilities, patches, or security-related features. The code examples include standard TLS connection parameters (tls=true, tlsCAFile) which are routine security best practices for database connections, but this is not the primary focus of the documentation change.
Diff
diff --git a/documentdb/latest/developerguide/arrayToObject.md b/documentdb/latest/developerguide/arrayToObject.md index 8b1378917..5e4e45483 100644 --- a//documentdb/latest/developerguide/arrayToObject.md +++ b//documentdb/latest/developerguide/arrayToObject.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#arrayToObject "Open PDF") @@ -1,0 +3,115 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $arrayToObject + +The `$arrayToObject` operator in Amazon DocumentDB is the reverse of the `$objectToArray` operator. It takes an array of key-value pair documents and converts it into a single document. This is particularly useful when you need to convert an array of key-value pairs back into an object or document structure. + +**Parameters** + + * `array expression`: An expression that resolves to an array. The array elements must be documents with two fields: `k` (the key) and `v` (the value). + + + + +## Example (MongoDB Shell) + +The example below demonstrates how to use `$arrayToObject` to convert an array of key-value pairs back into a document. + +**Create sample documents** + + + db.videos.insertMany([ + { "_id": 1, "name": "Live Soft", "inventory": { "Des Moines": 1000, "Ames": 500 } }, + { "_id": 2, "name": "Top Pilot", "inventory": { "Mason City": 250, "Des Moines": 1000 } }, + { "_id": 3, "name": "Romancing the Rock", "inventory": { "Mason City": 250, "Ames": 500 } }, + { "_id": 4, "name": "Bravemind", "inventory": { "Mason City": 250, "Des Moines": 1000, "Ames": 500 } } + ]); + +**Query example** + + + db.videos.aggregate([ + { $project: { name: 1, videos: { $objectToArray: "$inventory" } } }, + { $project: { name: 1, inventory: { $arrayToObject: "$videos" } } } + ]); + +**Output** + + + { "_id" : 1, "name" : "Live Soft", "inventory" : { "Des Moines" : 1000, "Ames" : 500 } } + { "_id" : 2, "name" : "Top Pilot", "inventory" : { "Mason City" : 250, "Des Moines" : 1000 } } + { "_id" : 3, "name" : "Romancing the Rock", "inventory" : { "Mason City" : 250, "Ames" : 500 } } + { "_id" : 4, "name" : "Bravemind", "inventory" : { "Mason City" : 250, "Des Moines" : 1000, "Ames" : 500 } } + +In this example, the `$objectToArray` operator is used to convert the `inventory` object into an array of key-value pairs. The `$arrayToObject` operator is then used to convert the array back into a document, restoring the original object structure. + +## Code examples + +To view a code example for using the `$arrayToObject` 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('videos'); + + const result = await collection.aggregate([ + { $project: { name: 1, videos: { $objectToArray: '$inventory' } } }, + { $project: { name: 1, inventory: { $arrayToObject: '$videos' } } } + ]).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['videos'] + + result = list(collection.aggregate([ + { '$project': { 'name': 1, 'videos': { '$objectToArray': '$inventory' } } }, + { '$project': { 'name': 1, 'inventory': { '$arrayToObject': '$videos' } } } + ])) + + print(result) + client.close() + + example() + + **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) + +$arrayElemAt + +$avg + +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.