In MongoDB you can use the $isArray aggregation pipeline operator to check whether or not a value is an array.
It accepts any valid expression, and returns true if the expression is an array, false if it’s not.
Example
Suppose we have a collection called test with the following documents:
{ "_id" : 1, "data" : [ ] }
{ "_id" : 2, "data" : [ 1, 2, 3 ] }
{ "_id" : 3, "data" : [ "XS", "M", "L" ] }
{ "_id" : 4, "data" : 123 }
{ "_id" : 5, "data" : "Homer Jay" }
We can use the following code to check the data field for arrays:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3, 4, 5 ] } } },
{
$project:
{
_id: 1,
data: 1,
result: { $isArray: "$data" }
}
}
]
)
Result:
{ "_id" : 1, "data" : [ ], "result" : true }
{ "_id" : 2, "data" : [ 1, 2, 3 ], "result" : true }
{ "_id" : 3, "data" : [ "XS", "M", "L" ], "result" : true }
{ "_id" : 4, "data" : 123, "result" : false }
{ "_id" : 5, "data" : "Homer Jay", "result" : false }
We can see that the first three documents contain arrays and the other two don’t.
Also, we can see that the first document contains an empty array. That is still an array, and so $isArray returns true.
Example 2
Here’s another example that contains various fields of differing BSON types.
Suppose we have a collection called types with the following document:
{
"_id" : ObjectId("601738d7c8eb4369cf6ad9de"),
"double" : 123.75,
"string" : "123",
"boolean" : true,
"date" : ISODate("2020-12-31T23:30:15.123Z"),
"integer" : 123,
"long" : NumberLong(123),
"decimal" : NumberDecimal("123.75"),
"object" : {
"a" : 1
},
"array" : [
1,
2,
3
]
}
For the purpose of this article, I’ve named each field to reflect its BSON type.
We can now use the following code to return either true or false for each field, depending on whether or not the field is an array:
db.types.aggregate(
[
{
$project:
{
_id: { $isArray: "$_id" },
double: { $isArray: "$double" },
string: { $isArray: "$string" },
boolean: { $isArray: "$boolean" },
date: { $isArray: "$date" },
integer: { $isArray: "$integer" },
long: { $isArray: "$long" },
decimal: { $isArray: "$decimal" },
object: { $isArray: "$object" },
array: { $isArray: "$array" }
}
}
]
).pretty()
Result:
{
"_id" : false,
"double" : false,
"string" : false,
"boolean" : false,
"date" : false,
"integer" : false,
"long" : false,
"decimal" : false,
"object" : false,
"array" : true
}
Missing Field
Applying $isArray to a field that doesn’t exist results in false.
Suppose our test collection contains the following document:
{ "_id" : 6 }
And we apply $isArray to that document:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 6 ] } } },
{
$project:
{
_id: 1,
data: 1,
result: { $isArray: "$data" }
}
}
]
)
Result:
{ "_id" : 6, "result" : false }
Null Values
Applying $isArray to null returns false.
Suppose our test collection contains the following document:
{ "_id" : 7, "data" : null }
And we apply $isArray to that document:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 7 ] } } },
{
$project:
{
_id: 1,
data: 1,
result: { $isArray: "$data" }
}
}
]
)
Result:
{ "_id" : 7, "data" : null, "result" : false }
Undefined Values
Likewise, supplying undefined values returns false.
Suppose our test collection contains the following document:
{ "_id" : 8, "data" : undefined }
Apply $isArray:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 8 ] } } },
{
$project:
{
_id: 1,
data: 1,
result: { $isArray: "$data" }
}
}
]
)
Result:
{ "_id" : 8, "data" : undefined, "result" : false }