On this page
$concatArrays (aggregation)
在本页面
Definition
$concatArrays
- 3.2 版中的新功能。
连接数组以返回连接的数组。
$concatArrays具有以下语法:
{ $concatArrays: [ <array1>, <array2>, ... ] }
<array>
表达式可以是任何有效的expression,只要它们解析为数组即可。有关表达式的更多信息,请参见Expressions。
如果有任何参数解析为null
的值或指向缺少的字段,则$concatArrays返回null
。
Behavior
Example | Results |
---|---|
{$ concatArrays:[ | |
[ “你好”, ” ”], [ "world"] ] } |
[ "hello", " ", "world" ] |
{$ concatArrays:[ [ “你好”, ” ”], [ [ "world"], "again"] ] } |
[ "hello", " ", [ "world" ], "again" ] |
Example
名为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: [ ] }
下面的示例连接instock
和ordered
数组:
db.warehouses.aggregate([
{ $project: { items: { $concatArrays: [ "$instock", "$ordered" ] } } }
])
{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : null }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
See also