The $isArray operator checks whether a value is an array. It returns true or false — useful for data validation, defensive pipeline logic, and filtering documents before applying array operators like $filter or $size.
01
Type Check
Test if value is array.
02
Syntax
One expression inside $isArray.
03
Boolean Output
Returns true or false.
04
$expr + $match
Filter array fields.
05
Use Cases
Validation, safe pipelines.
06
Empty Arrays
[] still returns true.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $isArray operator evaluates an expression and returns true when the result is a BSON array, and false for all other types (strings, numbers, objects, null, missing fields, and so on). This is especially helpful when your collection has inconsistent schemas — some documents may store tags as an array while others store a single string.
Use $isArray as a guard before array-specific operators. For example, only run $size or $filter when the field is actually an array, preventing pipeline errors on malformed documents.
💡
Beginner Tip
$isArray is an expression operator, not a direct query filter. To filter documents in $match, wrap it in $expr: { $match: { $expr: { $isArray: "$tags" } } }.
Foundation
📝 Syntax
The $isArray operator takes a single expression to evaluate:
mongosh
{ $isArray: <expression> }
Common Patterns
mongosh
// Check a document field
{ $isArray: "$tags" }
// Filter documents where tags is an array
{ $match: { $expr: { $isArray: "$tags" } } }
// Guard before array logic in $cond
{ $cond: [
{ $isArray: "$items" },
{ $size: "$items" },
0
] }
Syntax Rules
<expression> — can be a field path ("$tags"), a literal array, or another expression.
Returns true for arrays, including empty arrays [].
Returns false for strings, objects, numbers, booleans, null, and missing fields.
Use inside $project, $addFields, $cond, and $match (with $expr).
Does not work as a standalone query operator like { tags: { $isArray: true } }.
💡 Expression Operator vs Query Filter
$isArray must be used as an aggregation expression:
$not inverts the boolean result. Documents where tags is a string or missing get needsTagFix: true, making them easy to find and correct in a data cleanup job.
Bonus — Guard Before $filter
Only filter array elements when the input is actually an array:
If tags is an array, $filter removes empty strings. If it is not an array, the pipeline returns an empty array [] instead of failing. This defensive pattern keeps pipelines robust on messy data.
Applications
🚀 Use Cases
Schema validation — detect documents where a field should be an array but is stored as a string or object.
Defensive pipelines — guard $size, $filter, $map, and $indexOfArray with an $isArray check.
Data cleanup — find and flag malformed records before migration or ETL jobs.
Conditional processing — branch logic in $cond based on whether input is array-shaped.
🧠 How $isArray Works
1
MongoDB evaluates the expression
The pipeline resolves the input — a field like "$tags" or a literal value.
Input
2
$isArray checks the BSON type
MongoDB tests whether the value is a BSON array. Strings, objects, numbers, and null all return false.
Type check
3
The boolean result is applied
In $project, the result is stored in a field. In $match + $expr, only true documents pass.
Output
=
📊
Safer array pipelines
You get reliable type checks before running array operators on inconsistent data.
Wrap Up
Conclusion
The $isArray operator is a small but important type-checking tool in MongoDB aggregation pipelines. It lets you confirm that a value is an array before applying array-specific logic, which is essential when dealing with flexible schemas or legacy data.
For beginners, remember: wrap any expression in { $isArray: ... } inside a stage like $project or $cond. To filter documents, combine it with $expr in $match. Empty arrays count as arrays too.
Flag invalid documents with $not: { $isArray: ... } for cleanup
Remember empty arrays [] return true
Pair with $cond for safe fallback values
❌ Don’t
Use { field: { $isArray: true } } as a plain query filter
Assume a string that looks like a list is an array
Skip type checks on fields with inconsistent schemas
Confuse $isArray with $exists (presence vs type)
Expect $isArray to validate array element types
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $isArray
Use these points when validating array fields in MongoDB.
5
Core concepts
📦01
Type Check
Tests if value is array.
Purpose
📝02
Simple Syntax
{ $isArray: expr }
Syntax
🛠03
Boolean Output
true or false.
Result
🔒04
Safe Pipelines
Guard before $size/$filter.
Pattern
⚠05
Empty Arrays
[] is still true.
Edge case
❓ Frequently Asked Questions
$isArray returns true when the evaluated expression is a BSON array, and false otherwise. It is an aggregation expression operator used inside stages like $project, $addFields, and $cond.
The syntax is { $isArray: <expression> }. The expression can be a field reference like "$tags" or any other aggregation expression.
Yes. An empty array [] is still an array, so $isArray returns true. It checks the type, not whether elements exist.
Use $match with $expr: { $match: { $expr: { $isArray: "$tags" } } }. The plain query form { tags: { $isArray: true } } does not work — $isArray is an expression operator.
$isArray returns true or false for arrays only. $type returns the BSON type name or number (e.g. "array", "string", "object") and can identify many types, not just arrays.