文档菜单
文档首页
/
MongoDB 手册
/ / /

$toUpper (聚合)

在本页中

  • 定义
  • 行为
  • 示例
$toUpper

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

$toUpper 的语法如下

{ $toUpper: <expression> }

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

如果参数解析为 null,$toUpper 返回空字符串 ""

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

考虑一个包含以下文档的 inventory 集合

db.inventory.insertMany( [
{ "_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 }
] )

以下操作使用 $toUpper 运算符返回大写的 item 和大写的 description

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

操作返回以下结果

{ "_id" : 1, "item" : "ABC1", "description" : "PRODUCT 1" }
{ "_id" : 2, "item" : "ABC2", "description" : "PRODUCT 2" }
{ "_id" : 3, "item" : "XYZ1", "description" : "" }

返回

转换为小写

在本页中