Open In App

MongoDB $toUpper Operator

Improve
Improve
Like Article
Like
Save
Share
Report

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"}}}])


Last Updated : 01 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads