Database Manual / Reference / Query Language / Expressions

$concatArrays (expression operator)(表达式运算符)

Definition定义

Returns a single array that concatenates two or more arrays. $concatArrays can be used as an aggregation accumulator or an array operator.返回一个连接两个或多个数组的单个数组。$concatArrays可以用作聚合累加器或数组运算符。

Aggregation Accumulator聚合累加器

$concatArrays

$concatArrays is available as an accumulator in these stages:在以下阶段可作为蓄能器使用:

Syntax语法

When used as an aggregation accumulator, $concatArrays has the following syntax:当用作聚合累加器时,$concatArrays具有以下语法:

{ $concatArrays: "<array field>" }

Behavior行为

Arguments that are null or resolve to null are included in the results. Arguments that refer to a field that is missing are not included in the results.结果中包含null或解析为null的参数。引用缺失字段的参数不包括在结果中。

Example示例

Create a collection named sales with the following documents:使用以下文档创建一个名为sales的集合:

db.sales.insertMany( [
{
_id: 1,
items: [ "laptop", "tablet" ],
location: "NYC"
},
{
_id: 2,
items: [ "phone", "tablet" ],
location: "NYC"
},
{
_id: 3,
location: "NYC"
},
{
_id: 4,
items: [ "desktop", { "accessories": [ "mouse", "keyboard"] } ],
location: "NYC"
}
] )

This example shows how you can use $concatArrays as an accumulator. This example combines the elements of all items arrays when grouping on the location field:此示例显示了如何将$concatArrays用作累加器。此示例在location字段上分组时组合了所有items数组的元素:

db.sales.aggregate( [
{
$group: {
_id: "$location",
array: { "$concatArrays": "$items" }
}
}
] )

The operation returns the following result:该操作返回以下结果:

[
{
"_id": "NYC",
"array": [
"laptop", "tablet", "phone", "tablet", "desktop",
{ "accessories": [ "mouse", "keyboard"] }
]
}
]

Array Operator数组运算符

$concatArrays

Syntax语法

When used as an array operator, $concatArrays has the following syntax:当用作数组运算符时,$concatArrays具有以下语法:

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

Behavior行为

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

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

Example示例

Create a collection named warehouses with the following documents:使用以下文档创建一个名为warehouses的集合:

db.warehouses.insertMany( [
{ _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" ] } } }
] )

The operation returns the following results:该操作返回以下结果:

[
{ _id : 1, items : [ "chocolate", "butter", "apples" ] },
{ _id : 2, items : null },
{ _id : 3, items : [ "pears", "pecans", "cherries" ] },
{ _id : 4, items : [ "ice cream" ] }
]

Limitations局限性

$concatArrays only supports arrays and expressions that resolve to an array.仅支持解析为数组的数组和表达式。

Learn More了解更多