$isArray
The $isArray operator in Amazon DocumentDB is used to check if a field in a document is an array. This operator can be useful in aggregation pipelines and conditional expressions to handle array-type fields.
Parameters
-
field: The field path to check if it is an array.
Example (MongoDB Shell)
This example demonstrates how to use the $isArray operator to identify documents where the "inventory" field is an array.
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": [{"location": "Mason City", "count": 250}, {"location": "Des Moines", "count": 1000}, {"location": "Ames", "count": 500}]} ]);
Query example
db.videos.aggregate([ { $match: { $isArray: "$inventory" } }, { $project: { _id: 1, name: 1, "inventory.location": 1, "inventory.count": 1 } } ]).pretty();
Output
{
"_id": 4,
"name": "Bravemind",
"inventory": [
{
"location": "Mason City",
"count": 250
},
{
"location": "Des Moines",
"count": 1000
},
{
"location": "Ames",
"count": 500
}
]
}
Code examples
To view a code example for using the $isArray command, choose the tab for the language that you want to use: