$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) andv(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: