$toUpper (aggregation)

On this page本页内容

Definition定义

$toUpper

Converts a string to uppercase, returning the result.将字符串转换为大写,返回结果。

$toUpper has the following syntax:语法如下:

{ $toUpper: <expression> }

The argument can be any expression as long as it resolves to a string. 参数可以是任何表达式,只要它解析为字符串。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

If the argument resolves to null, $toUpper returns an empty string "".如果参数解析为null$toUpper将返回空字符串""

Behavior行为

$toUpper only has a well-defined behavior for strings of ASCII characters.对于ASCII字符字符串,只有定义良好的行为。

Example示例

Consider a inventory collection with the following documents:考虑使用以下文档进行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 }

The following operation uses the $toUpper operator return uppercase item and uppercase description values:以下操作使用$toUpper运算符返回大写item和大写description值:

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

The operation returns the following results:该操作返回以下结果:

{ "_id" : 1, "item" : "ABC1", "description" : "PRODUCT 1" }
{ "_id" : 2, "item" : "ABC2", "description" : "PRODUCT 2" }
{ "_id" : 3, "item" : "XYZ1", "description" : "" }
←  $toLower (aggregation)$tsIncrement (aggregation) →