Skip to content
Related Articles

Related Articles

MongoDB $toUpper Operator

View Discussion
Improve Article
Save Article
  • Last Updated : 01 Aug, 2020
View Discussion
Improve Article
Save Article

MongoDB provides different types of string expression operators that are used in the aggregation pipeline stages $toUpper operator is one of them. This operator is used to convert the given string into uppercase.

Syntax: 

{ $toUpper: <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 $toUpper Operator:

In this example, we are going to convert the value of the department field in uppercase and assign the result of the $toUpper operator as a value of dept field.

db.employee.aggregate([
... {$project: {dept: {$toUpper: "$department"}}}])

Using $toUpper Operator in the Embedded Document:

In this example, we are going to convert the value of the name.first field in uppercase and assign the result of the $toUpper operator as a value of firstName field.

db.employee.aggregate([
... {$project: {firstName: {$toUpper: "$name.first"}}}])

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!