$concatArrays (aggregation)

On this page本页内容

Definition定义

$concatArrays

New in version 3.2.版本3.2中的新功能。

Concatenates arrays to return the concatenated array.连接数组以返回连接的数组。

$concatArrays has the following syntax:语法如下所示:

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

The <array> expressions can be any valid expression as long as they resolve to an array. <array>表达式可以是任何有效的表达式,只要它们解析为数组。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

If any argument resolves to a value of null or refers to a field that is missing, $concatArrays returns null.如果任何参数解析为null值或引用缺少的字段,$concatArrays将返回null

Behavior行为

Example示例Results结果
{ $concatArrays: [
   [ "hello", " "], [ "world" ]
] }
[ "hello", " ", "world" ]
{ $concatArrays: [
   [ "hello", " "],
   [ [ "world" ], "again"]
] }
[ "hello", " ", [ "world" ], "again" ]

Example示例

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 concatenates the instock and the ordered arrays:以下示例将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" ] }

See also参阅

$push