MongoDB $isArray Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Type Check

What You’ll Learn

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.

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" } } }.

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

$project{ tagsIsArray: { $isArray: "$tags" } } (adds boolean field)
$match{ $expr: { $isArray: "$tags" } } (filters documents)
Invalid{ tags: { $isArray: true } } (will not work)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (type check)
Syntax{ $isArray: <expression> }
Outputtrue or false
Empty array []Returns true
Common stages$project, $addFields, $cond, $match + $expr
Field check
{
  $isArray: "$tags"
}

true if tags is array

Literal array
{
  $isArray: [1, 2, 3]
}

Returns true

String field
{
  $isArray: "$name"
}

Returns false

$match filter
{
  $expr: {
    $isArray: "$items"
  }
}

Keep array fields only

Examples Gallery

Walk through sample product data with mixed field types, validate arrays with $project, and filter safely in $match.

📚 Check Field Types

Start with a products collection where the tags field is not always stored as an array.

Sample Input Documents

Some documents have tags as an array, others as a string, and one has no tags field:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "MongoDB Guide", "tags": ["database", "mongodb"] },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Chess Set", "tags": "games" },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Coffee Mug", "tags": [] },
  { "_id": ObjectId("609c26812e9274a86871bc6d"), "name": "Notebook" }
]

Example 1 — Basic $isArray on a Field

Add a boolean field showing whether tags is stored as an array:

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      tags: 1,
      tagsIsArray: {
        $isArray: "$tags"
      }
    }
  }
])

How It Works

  • Arrays with elements and empty arrays both return true.
  • A string value like "games" returns false even though it looks list-like.
  • Missing fields evaluate to false because they are not arrays.

📈 Practical Patterns

Filter valid array documents, guard array operators, and flag data quality issues.

Example 2 — Filter with $match and $expr

Keep only documents where tags is actually an array:

mongosh
db.products.aggregate([
  {
    $match: {
      $expr: {
        $isArray: "$tags"
      }
    }
  },
  {
    $project: {
      name: 1,
      tags: 1
    }
  }
])

How It Works

$expr lets you use aggregation expressions inside $match. Only documents where $isArray returns true pass through to the next stage.

Example 3 — Safe $size with $cond

Count tags only when the field is an array; otherwise return 0:

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      tagCount: {
        $cond: [
          { $isArray: "$tags" },
          { $size: "$tags" },
          0
        ]
      }
    }
  }
])

How It Works

$size only works on arrays. Wrapping it in $cond with $isArray prevents errors when the field is a string, object, or missing.

Example 4 — Flag Invalid Data for Cleanup

Mark documents that need schema fixes when tags is not an array:

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      tags: 1,
      needsTagFix: {
        $not: {
          $isArray: "$tags"
        }
      }
    }
  },
  {
    $match: {
      needsTagFix: true
    }
  }
])

How It Works

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

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      activeTags: {
        $cond: [
          { $isArray: "$tags" },
          {
            $filter: {
              input: "$tags",
              as: "tag",
              cond: { $ne: [ "$$tag", "" ] }
            }
          },
          []
        ]
      }
    }
  }
])

How It Works

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.

🚀 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.

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.

💡 Best Practices

✅ Do

  • Guard $size, $filter, and $map with $isArray
  • Use $match + $expr to filter array-shaped fields
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $isArray

Use these points when validating array fields in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $isNumber for numeric type checks, or review $filter for array processing.

Next: $isNumber →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful