Docs Menu
Docs Home
/
MongoDB Manual
/ / /

$toLower (aggregation)

On this page

  • Definition
  • Behavior
  • Example
$toLower

Converts a string to lowercase, returning the result.

$toLower has the following syntax:

{ $toLower: <expression> }

The argument can be any expression as long as it resolves to a string. For more information on expressions, see Expression Operators.

If the argument resolves to null, $toLower returns an empty string "".

$toLower only has a well-defined behavior for strings of ASCII characters.

Consider a inventory collection with the following documents:

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 }
] )

The following operation uses the $toLower operator return lowercase item and lowercase description value:

db.inventory.aggregate(
[
{
$project:
{
item: { $toLower: "$item" },
description: { $toLower: "$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" : "" }

Back

$toString