AggregateCommand.concatArrays(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。将多个数组拼接成一个数组。

参数

value: Expression[]

[ <array1>, <array2>, … ]

返回值

Object

API 说明

语法如下:

  1. db.command.aggregate.arrayToObject([ <array1>, <array2>, ... ])

参数可以是任意解析为数组的表达式。

示例代码

假设集合 items 有如下记录:

  1. { "_id": 1, "fruits": [ "apple" ], "vegetables": [ "carrot" ] }
  2. { "_id": 2, "fruits": [ "orange", "lemon" ], "vegetables": [ "cabbage" ] }
  3. { "_id": 3, "fruits": [ "strawberry" ], "vegetables": [ "spinach" ] }
  1. const $ = db.command.aggregate
  2. db.collection('items').aggregate()
  3. .project({
  4. list: $.concatArrays(['$fruits', '$vegetables']),
  5. })
  6. .end()

返回结果如下:

  1. { "_id": 1, "list": [ "apple", "carrot" ] }
  2. { "_id": 2, "list": [ "orange", "lemon", "cabbage" ] }
  3. { "_id": 3, "list": [ "strawberry", "spinach" ] }