MongoDB $toLower Operator
MongoDB provides different types of string expression operators that are used in the aggregation pipeline stages $toLower operator is one of them. This operator is used to convert the given string into lowercase.
Syntax:
{ $toLower: <expression> }
Here, the argument passed in this operator can be any valid expression until they resolve to a string. If the entered argument resolves to null, then this operator will return an empty string “”.
Examples:
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.
Using $toLower Operator:
In this example, we are going to convert the value of the department field in lowercase.
db.employee.aggregate([ ... {$match: {"name.first": "Aman"}}, ... {$project: {dept: {$toLower: "$department"}}}])
Using $toLower Operator in embedded documents:
In this example, we are going to convert the value of the name.first field in lowercase.
db.employee.aggregate([ ... {$match: {department: "Development"}}, ... {$project: {name: {$toLower: "$name.first"}}}])
Please Login to comment...