$toUpper (Uppercase)
Last updated: 2026-10-09
$toUpper (Uppercase)
$toUpper converts a string to uppercase. It is the simplest string transformation operator, often used to normalize comparisons or unify output formats. It only accepts strings — other types throw (convert with $toString first).
| Category | String |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $toUpper: <string-expression> }
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $toUpper: uppercase city names
db.users.aggregate([
{ $project: { cityUpper: { $toUpper: "$city" } } }
])
Output:
{ "_id": 1, "cityUpper": "BEIJING" }
{ "_id": 2, "cityUpper": "SHANGHAI" }
📌 Notes
- Strings only; convert other types first
- Inverse of $toLower
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QDoes $toUpper affect non-Latin characters like Chinese?
ANo. $toUpper only affects Latin letters (A–Z); characters without case, such as Chinese, Japanese or Arabic, are returned unchanged.
QWhat does $toUpper return for a missing or null field?
AIt returns null. The expression does not abort; chain $ifNull afterwards to handle nulls.
QCan $toUpper and $toLower be chained?
AYes, but it is pointless — the latter overwrites the former. A common pattern is combining with $regexMatch for case-insensitive comparisons.