$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).

CategoryString
MongoDB Version1.0+
Example Languagemongosh (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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏