$toUpper
The $toUpper operator converts a string to all uppercase letters.
📌 Syntax
{ "$toUpper": <expression> }
The expression must resolve to a string.
✅ Base Example 1 – Uppercase City Name
📥 Input Document
{ "city": "toronto" }
📌 Expression
{ "$toUpper": "$city" }
📤 Output
"TORONTO"
✅ Base Example 2 – Uppercase Username for Display
📥 Input Document
{ "username": "janedoe" }
📌 Expression
{ "$toUpper": "$username" }
📤 Output
"JANEDOE"
🧱 Ecommerce Example – Uppercase Category Labels
📌 Pipeline
[
{ "$unwind": "$items" },
{
"$project": {
"categoryUpper": { "$toUpper": "$items.category" },
"product": "$items.name"
}
}
]
📥 Input Document
{
"items": [
{ "name": "Shoes", "category": "fashion" },
{ "name": "Tablet", "category": "electronics" }
]
}
📤 Output
[
{ "categoryUpper": "FASHION", "product": "Shoes" },
{ "categoryUpper": "ELECTRONICS", "product": "Tablet" }
]
🔧 Common Use Cases
- Case normalization
- Visual formatting for UI
- Standardizing categories and labels
🔗 Related Operators
$toLower,$trim,$concat,$substr,$toString
🧠Notes
- Input must be a string.
- Combine with
$ifNullor$condto avoid null errors.