The $isArray operator in MongoDB checks whether a given field or expression evaluates to an array and returns a boolean result, helping handle mixed data types safely in aggregation pipelines.
Type Check: Returns true if the value is an array, otherwise false.
Handles Mixed Data: Useful when fields may contain arrays, strings, or numbers.
Expression-Based: Works within aggregation expressions (and in queries only via $expr).
Validation Support: Helps avoid errors when processing array-specific operations.
Syntax
{ $isArray: <expression> }
$isArray: Evaluates the given expression and checks whether it is an array.
<expression>: The field or expression whose type is being tested for array.
Usage of $isArray Operator
The $isArray operator is useful in scenarios where:
When document fields may contain arrays or single values.
To conditionally run array operations like $size, $push, or updates.
To prevent errors by validating field types before processing.
To build dynamic queries that adapt to mixed data types.
Useful in aggregation pipelines for type-safe transformations.
Examples of MongoDB $isArray Operator
Consider the following collection named products used for the examples below.