$toLower (aggregation)

Definition

  • $toLower
  • Converts a string to lowercase, returning the result.

$toLower has the following syntax:

  1. { $toLower: <expression> }

The argument can be any expressionas long as it resolves to a string. For more information onexpressions, see Expressions.

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

Behavior

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

Example

Consider a inventory collection with the following documents:

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

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

  1. db.inventory.aggregate(
  2. [
  3. {
  4. $project:
  5. {
  6. item: { $toLower: "$item" },
  7. description: { $toLower: "$description" }
  8. }
  9. }
  10. ]
  11. )

The operation returns the following results:

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