“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$toLower(聚合)

在此页面上

  • 定义
  • 行为
  • 例子
$toLower

将字符串转换为小写,并返回结果。

$toLower 通过以下语法实现:

{ $toLower: <expression> }

该参数可以是任何表达式,只要它解析为字符串即可。有关表达式的更多信息,请参阅表达式操作符。

如果参数解析为空值,$toLower 将返回一个空字符串 ""

$toLower 仅对 ASCII 字符字符串有明确定义的行为。

请考虑包含以下文档的 inventory 集合:

{ "_id" : 1, "item" : "ABC1", quarter: "13Q1", "description" : "PRODUCT 1" }
{ "_id" : 2, "item" : "abc2", quarter: "13Q4", "description" : "Product 2" }
{ "_id" : 3, "item" : "xyz1", quarter: "14Q2", "description" : null }

以下操作使用 $toLower 操作符返回小写 item 和小写 description 值:

db.inventory.aggregate(
[
{
$project:
{
item: { $toLower: "$item" },
description: { $toLower: "$description" }
}
}
]
)

操作返回以下结果:

{ "_id" : 1, "item" : "abc1", "description" : "product 1" }
{ "_id" : 2, "item" : "abc2", "description" : "product 2" }
{ "_id" : 3, "item" : "xyz1", "description" : "" }

后退

$toString(聚合)

来年

$toUpper(聚合)

在此页面上