$concatArrays (aggregation)

    在本页面

    $concatArrays

    3.2版中的新功能。

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

    $concatArrays具有以下语法:

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

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

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

    行为

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

    例子

    名为的集合warehouses包含以下文档:

    1. { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }
    2. { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }
    3. { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }
    4. { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

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

    1. db.warehouses.aggregate([
    2. { $project: { items: { $concatArrays: [ "$instock", "$ordered" ] } } }
    3. ])
    1. { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
    2. { "_id" : 2, "items" : null }
    3. { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
    4. { "_id" : 4, "items" : [ "ice cream" ] }

    也可以看看

    $push

    译者:李冠飞

    校对: