$objectToArray
The $objectToArray aggregation operator in Amazon DocumentDB converts an object (or document) into an array. The input to the operator is a document, and the output consists of an array element for each field-value pair in the input document. This operator is useful when you need to work with the individual fields of a document as an array, such as when you want to find the document with the maximum or minimum value for a particular field.
Parameters
-
expression: The document expression to convert into an array.
Example (MongoDB Shell)
The following example demonstrates how to use the $objectToArray operator to find the document with the maximum inventory for a video rental store chain.
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" } } }, { $unwind: "$videos" }, { $group: { _id: "$name", maxInventory: { $max: "$videos.v" } } } ]);
Output
[
{
"_id": "Bravemind",
"maxInventory": 1000
},
{
"_id": "Live Soft",
"maxInventory": 1000
},
{
"_id": "Romancing the Rock",
"maxInventory": 500
},
{
"_id": "Top Pilot",
"maxInventory": 1000
}
]
Code examples
To view a code example for using the $objectToArray command, choose the tab for the language that you want to use: