On this page本页内容
$isArray
Determines if the operand is an array. Returns a boolean.确定操作数是否为数组。返回布尔值。
$isArray
has the following syntax:语法如下:
{ $isArray: [ <expression> ] }
The <expression>
can be any valid expression. <expression>
可以是任何有效的表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
{ $isArray: [ "hello" ] } | false |
{ $isArray: [ [ "hello", "world" ] ] } | true |
A collection named 名为warehouses
contains the following documents:warehouses
的集合包含以下文档:
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
The following example checks if the 以下示例在连接instock
and the ordered
fields are arrays before concatenating the two:instock
和ordered
字段之前检查它们是否为数组:
db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ])
{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }