对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$concatArrays(表达式操作符)

$concatArrays

连接数组以返回连接后的数组。

$concatArrays 通过以下语法实现:

{ $concatArrays: [ <expression1>, <expression2>, ... ] }

<array> 表达式可以是任何有效的表达式,只要它们解析为数组即可。有关表达式的更多信息,请参阅表达式。

如果任一参数能解析为 null 值或引用了缺失的字段,$concatArrays 则会返回 null。

例子
结果
{ $concatArrays: [ [ "hello", " "], [ "world" ]] }
[ "hello", " ", "world" ]
{ $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"]] }
[ "hello", " ", [ "world" ], "again" ]

使用以下文档创建名为 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: [ ] }] )

以下示例会将 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" ] }

提示

$push

给本页内容打分

在此页面上