The $toUpper operator converts string values to uppercase in MongoDB aggregation pipelines. Use it to standardize country codes, status labels, category names, and display text for consistent formatting.
01
Uppercase
Text to UPPER.
02
Syntax
One expression.
03
Standardize
Codes & labels.
04
$project Stage
Transform fields.
05
Use Cases
Display, grouping.
06
vs $toLower
Upper vs lower.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $toUpper operator converts a string expression to uppercase. For example, "john doe" becomes "JOHN DOE". This is useful when you need consistent formatting for country codes, status values, SKU prefixes, or report headers.
$toUpper is the counterpart of $toLower. Both are string transformation operators — they change letter casing without changing the BSON type. Numbers and symbols in the string are not affected.
💡
Beginner Tip
Think of $toUpper as MongoDB’s pipeline version of JavaScript’s .toUpperCase(). For emails and search keys, prefer $toLower instead.
Foundation
📝 Syntax
The $toUpper operator takes one string expression:
mongosh
{ $toUpper: <expression> }
Literal Examples
mongosh
{ $toUpper: "hello world" }
// Result: "HELLO WORLD"
{ $toUpper: "$name" }
// Uppercase the name field
{ $toUpper: "$countryCode" }
// "us" → "US"
Syntax Rules
$toUpper — returns the uppercase version of the string expression.
<expression> — a field path, literal string, or nested string expression.
null — returns null.
Only letter casing changes; numbers, spaces, and symbols stay the same.
Use inside $project, $addFields, or $set.
Unicode letters are uppercased according to MongoDB’s string rules.
💡 $toUpper vs $toLower vs $strcasecmp
$toUpper — converts a string to all uppercase
$toLower — converts a string to all lowercase (better for emails and search)
$strcasecmp — compares two strings case-insensitively without changing values
$map applies $toUpper to each element in the tags array individually.
Applications
🚀 Use Cases
Country and region codes — standardize ISO codes to uppercase (US, IN, GB).
Status and enum labels — normalize order status, role names, and category values.
Report headers — display section titles and labels in uppercase for readability.
Case-insensitive grouping — merge department or category values regardless of casing.
SKU and code prefixes — ensure product codes follow an uppercase convention.
🧠 How $toUpper Works
1
MongoDB reads the expression
The input resolves from a field path, literal string, or nested expression like $concat.
Input
2
Letters are uppercased
Lowercase and mixed-case letters become uppercase. Numbers and punctuation stay unchanged.
Transform
3
An uppercase string is returned
The result is stored in your pipeline field for display, grouping, or further processing.
Output
=
📝
Standardized text
Use for codes, labels, grouping keys, and consistent display formatting.
Wrap Up
Conclusion
The $toUpper operator converts strings to uppercase in MongoDB aggregation pipelines. It is ideal for standardizing country codes, status labels, and display headers, and for case-insensitive grouping.
For emails and search normalization, use $toLower instead. For case-insensitive comparison without changing values, use $strcasecmp. Next in the series: $trim.
Mix uppercase and lowercase normalization on the same field type
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $toUpper
Use these points when uppercasing text in MongoDB pipelines.
5
Core concepts
📝01
Uppercase
Text transform.
Purpose
📝02
{ $toUpper: x }
One argument.
Syntax
🌐03
Country codes
us → US.
Use case
🛠04
$group key
Merge casing.
Pattern
⚠05
vs $toLower
Emails: lower.
Compare
❓ Frequently Asked Questions
$toUpper converts a string value to uppercase inside an aggregation pipeline. It is an aggregation expression operator used in stages like $project and $addFields.
The syntax is { $toUpper: <expression> }. The expression can be a field reference like "$name", a literal string, or another string expression.
$toUpper converts text to all uppercase letters (e.g. "john" → "JOHN"). $toLower converts text to all lowercase letters. Choose based on your normalization convention.
Use $toUpper for country codes, status labels, and display headers. Use $toLower for emails and search keys. Pick one convention and apply it consistently across your pipeline.
$toUpper returns null when the input is null. It does not throw an error.