On this page本页内容
$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。
{ $concatArrays: [
[ "hello", " "], [ "world" ]
] }
| [ "hello", " ", "world" ] |
{ $concatArrays: [
[ "hello", " "],
[ [ "world" ], "again"]
] }
| [ "hello", " ", [ "world" ], "again" ] |
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: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" ] }