The $toUpper operator converts string values to uppercase within the aggregation pipeline, enabling consistent text normalization, formatting, and case-insensitive operations.
Converts all alphabetic characters in a string to uppercase.
Leaves numbers and special characters unchanged.
Accepts string expressions, including fields and embedded document fields.
Works on string expressions; non-string values must be explicitly converted using operators like $toString before applying $toUpper.
Commonly used for normalization, case-insensitive comparisons, and uniform display formatting in aggregation stages (e.g., $project, $addFields).
Syntax
{ $toUpper: <expression> }
<expression> : Any value or field that resolves to a string and will be converted to uppercase.
Null Handling: If the expression resolves to null, $toUpper returns null, not an empty string.
Use Cases for MongoDB $toUpper Operator
The $toUpper operator is versatile and can be used in various scenarios:
Case Normalization: Ensure uniformity in text fields by converting all text data to uppercase.
Case-Insensitive Comparisons: Use it for comparing strings without worrying about their case (e.g., "apple" vs. "APPLE").
Data Formatting: Display data in a specific format (e.g., converting names or addresses to uppercase for consistency).
Non-String Fields: To use $toUpper on non-string fields (e.g., numbers or dates), first convert them using $toString, then apply $toUpper.
Examples of MongoDB $toUpper Operator
Sample collection employee in the GeeksforGeeks database, where each document contains employee details like name and department.
Database: GeeksforGeeks
Collection: employee
Document: Three documents that contain the details of the employees in the form of field-value pairs.
Convert the value of the department field in uppercase and assign the result of the $toUpper operator as a value of dept field. This can be useful when we need to display or compare department names in a consistent case.