$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
returnsnull
.null
值或引用了缺失的字段,$concatArray
将返回null
。
Behavior行为
{ $concatArrays: [ |
[ "hello", " ", "world" ] |
{ $concatArrays: [ |
[ "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:instock
和ordered
数组:
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