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

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

聚合操作符。连接字符串,返回拼接后的字符串。

参数

value: Expression[]

[<表达式1>, <表达式2>, …]

返回值

Object

API 说明

concat 的语法如下:

  1. db.command.aggregate.concat([<表达式1>, <表达式2>, ...])

表达式可以是形如 $ + 指定字段,也可以是普通字符串。只要能够被解析成字符串即可。

示例代码

假设集合 students 的记录如下:

  1. { "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
  2. { "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
  3. { "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

借助 concat 可以拼接 lastNamefirstName 字段,得到每位学生的名字全称:

  1. const $ = db.command.aggregate
  2. db
  3. .collection('students')
  4. .aggregate()
  5. .project({
  6. _id: 0,
  7. fullName: $.concat(['$firstName', ' ', '$lastName'])
  8. })
  9. .end()

返回的结果如下:

  1. { "fullName": "Yuanxin Dong" }
  2. { "fullName": "Weijia Wang" }
  3. { "fullName": "Chengxi Li" }