db.command.aggregate.toLower

将字符串转化为小写并返回。

toLower 的语法如下:

db.command.aggregate.toLower(表达式)

只要表达式可以被解析成字符串,那么它就是有效表达式。例如:$ + 指定字段

示例

假设集合 students 的记录如下:

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

借助 toLowerfirstName 的字段值转化为小写:

const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .project({
    _id: 0,
    result: $.toLower('$firstName'),
  })
  .end()

返回的结果如下:

{ "result": "yuanxin" }
{ "result": "weijia" }
{ "result": "chengxi" }