Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 16 February 2024

LOWER Function in MongoDB

In MongoDB, there is no built-in function like LOWER() in SQL databases to convert strings to lowercase directly within the query language. However, you can achieve the same result using the aggregation framework along with $toLower operator available in aggregation expressions.

Here's an example of how you can achieve the lowercase conversion in MongoDB:

Suppose you have a collection named employees with documents containing a field first_name, and you want to retrieve the first names of all employees in lowercase:

db.employees.aggregate([

    {

        $project: {

            lowercase_first_name: { $toLower: "$first_name" }

        }

    }

])

This aggregation pipeline uses the $project stage to create a new field lowercase_first_name where the value is the lowercase conversion of the first_name field. The $toLower operator is used to convert the string to lowercase.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.