$isArray - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function run() { 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([ { $match: { $isArray: '$inventory' } }, { $project: { _id: 1, name: 1, "inventory.location": 1, "inventory.count": 1 } } ]).toArray(); console.log(result); await client.close(); } run();
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['videos'] result = list(collection.aggregate([ { '$match': { '$isArray': '$inventory' } }, { '$project': { '_id': 1, 'name': 1, 'inventory.location': 1, 'inventory.count': 1 } } ])) print(result) client.close()