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

Friday, 16 February 2024

UPPER Function in MongoDB

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

Here's an example of how you can achieve the uppercase 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 uppercase:

db.employees.aggregate([

    {

        $project: {

            uppercase_first_name: { $toUpper: "$first_name" }

        }

    }

])

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

No comments:

Post a Comment

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