The Wayback Machine - https://web.archive.org/web/20250305194137/https://www.geeksforgeeks.org/mongodb-toupper-operator/
Open In App

MongoDB $toUpper Operator

Last Updated : 24 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The MongoDB $toUpper operator is a powerful tool used to convert string values to uppercase within the aggregation framework. It is particularly useful when working with text data that requires case normalization.

In this article, We will learn about the MongoDB $toUpper Operator by understanding various examples in detail.

MongoDB $toUpper Operator

  • MongoDB $toUpper Operator is used to convert a string to uppercase.
  • It can also be used on non-string fields if they can be converted to a string but Its behavior for non-ASCII characters may not be as expected.
  • It Only affects alphabetic characters, non-alphabetic characters remain unchanged.

Syntax 

{ $toUpper: <expression> }

Here, the argument passed in this operator can be any valid expression until it resolves to a string. If the entered argument resolves to null, then this operator will return an empty string ” “.

Examples of MongoDB $toUpper Operator

To understand it better, let’s look at some examples of MongoDB $toUpper operator.

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.

Output:

demo database and collection

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

Output:

using $toupper operator example output

Example 2: Using $toUpper 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"}}}])

Output:

using $toupper operator in the embedded document example output

KeyTakeAways About MongoDB $toUpper Operator

  • The $toUpper operator is used in the aggregation pipeline stages to convert a string to uppercase and return the result.
  • It accepts an expression as an argument, which can be any expression that resolves to a string. This means $toUpper can be used on non-string fields as well, as long as they can be converted to a string.
  • If the argument provided to $toUpper resolves to null, it will return an empty string.
  • Its behavior for non-ASCII characters may not be as expected.
  • The $toUpper operator is designed for use within the aggregation framework and cannot be used directly in update operations to modify documents in a collection.
  • The $toLower operator, which converts a string to lowercase works similarly to $toUpper.

Conclusion

The MongoDB $toUpper operator is a versatile tool for string manipulation within the aggregation pipeline. By understanding its syntax, behavior, and application through examples, users can effectively use this operator to normalize and manage text data in their MongoDB collections.

FAQs on MongoDB $toUpper Operator

Can $toUpper be used directly in update operations?

No, the $toUpper operator is designed for use within the aggregation framework and cannot be used directly in update operations to modify documents in a collection.

What happens if the input to $toUpper is null?

If the argument provided to $toUpper resolves to null, it will return an empty string "".

How does $toUpper handle non-string fields?

The $toUpper operator can be used on non-string fields if they can be converted to a string. However, its behavior for non-ASCII characters may not be as expected.

Is there an equivalent operator to convert strings to lowercase in MongoDB?

Yes, the $toLower operator converts a string to lowercase and works similarly to $toUpper.


Next Article

Similar Reads

three90RightbarBannerImg