$concatArrays (aggregation)

在本页面

$concatArrays

3.2版中的新功能。

连接数组以返回连接的数组。

$concatArrays具有以下语法:

{ $concatArrays: [ <array1>, <array2>, ... ] }

<array>表达式可以是任何有效的表达式,只要它们解析为一个数组。有关表达式的更多信息,请参见表达式。

如果有任何参数解析为null或指向缺少的字段,则$concatArrays返回null

行为

例子 结果
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ]
{ $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

例子

名为的集合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: [ ] }

以下示例将instockordered 数组串联在一起:

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

也可以看看

$push

译者:李冠飞

校对:

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2020-12-18 11:34:57

results matching ""

    No results matching ""