Docs HomeMongoDB Manual

$concatArrays (aggregation)

On this page本页内容

Definition定义

$concatArrays

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值或引用了缺失的字段,$concatArray将返回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:名为仓库的集合包含以下文档:

{ "_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" ] }
Tip

See also: 另请参阅:

$push