MongoDB $toUpper Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Transform

What You’ll Learn

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.

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.

📝 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
See the $toLower operator tutorial for the lowercase counterpart

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (string transform)
Syntax{ $toUpper: <expression> }
InputString or string expression
OutputUppercase string (or null)
null inputReturns null
Common stages$project, $addFields, $set
Field
{
  $toUpper: "$name"
}

Uppercase a field

Literal
{
  $toUpper: "abc"
}

Returns "ABC"

Code
{
  $toUpper: "$country"
}

"in" → "IN"

Status
{
  $toUpper: "$status"
}

"active" → "ACTIVE"

Examples Gallery

Uppercase employee names, standardize country codes and status labels, and group categories case-insensitively with $toUpper.

📚 Employee Names

Start with an employees collection and convert names to uppercase with $project.

Sample Input Documents

Suppose you have an employees collection with lowercase names:

mongosh
[
  { "_id": 1, "name": "john doe",   "department": "engineering" },
  { "_id": 2, "name": "alice smith", "department": "marketing" },
  { "_id": 3, "name": "bob jones",  "department": "Engineering" }
]

Example 1 — Basic $toUpper on a Field

Convert each employee name to uppercase:

mongosh
db.employees.aggregate([
  {
    $project: {
      name: 1,
      upperName: { $toUpper: "$name" }
    }
  }
])

How It Works

$project keeps the original name and adds upperName with all letters converted to uppercase.

📈 Normalization Patterns

Standardize codes, status values, and group keys with uppercase conversion.

Example 2 — Standardize Country Codes

Country codes are conventionally uppercase (ISO 3166):

mongosh
db.customers.aggregate([
  {
    $addFields: {
      countryCode: { $toUpper: "$country" }
    }
  }
])

// country: "us"  → countryCode: "US"
// country: "In"  → countryCode: "IN"
// country: "gb"  → countryCode: "GB"

How It Works

Uppercasing country codes ensures consistent formatting regardless of how users entered the data.

Example 3 — Normalize Status Labels

Convert mixed-case status values to uppercase for reporting:

mongosh
db.orders.aggregate([
  {
    $project: {
      orderId: 1,
      status: 1,
      statusUpper: { $toUpper: "$status" }
    }
  }
])

// status: "pending"  → statusUpper: "PENDING"
// status: "Shipped"  → statusUpper: "SHIPPED"
// status: "CANCELLED" → statusUpper: "CANCELLED"

How It Works

Uppercase status labels are easier to read in dashboards and export reports where consistency matters.

Example 4 — Case-Insensitive Grouping by Department

Group employees by uppercase department to merge mixed-case entries:

mongosh
db.employees.aggregate([
  {
    $group: {
      _id: { $toUpper: "$department" },
      count: { $sum: 1 },
      employees: { $push: "$name" }
    }
  },
  {
    $sort: { count: -1 }
  }
])

// "engineering" + "Engineering" → grouped under "ENGINEERING"

How It Works

Using $toUpper as the _id in $group merges values that differ only by letter casing.

Example 5 — Display Label for Reports

Build an uppercase display header from a product name:

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      displayHeader: {
        $concat: [
          { $toUpper: "$category" },
          ": ",
          "$name"
        ]
      }
    }
  }
])

// category: "widget", name: "Pro Model"
// → displayHeader: "WIDGET: Pro Model"

How It Works

Combine $toUpper with $concat to create formatted labels where the category appears in uppercase.

Bonus — Uppercase Tags in an Array

Transform each tag in an array with $map:

mongosh
db.articles.aggregate([
  {
    $project: {
      title: 1,
      tagsUpper: {
        $map: {
          input: "$tags",
          as: "tag",
          in: { $toUpper: "$$tag" }
        }
      }
    }
  }
])

// tags: ["mongodb", "Tutorial", "aggregation"]
// → tagsUpper: ["MONGODB", "TUTORIAL", "AGGREGATION"]

How It Works

$map applies $toUpper to each element in the tags array individually.

🚀 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.

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.

💡 Best Practices

✅ Do

  • Use $toUpper for country codes and status labels
  • Apply in $group keys for case-insensitive aggregation
  • Keep original fields when display casing matters
  • Use $map to uppercase each element in string arrays
  • Pick uppercase or lowercase as a team convention and stick to it

❌ Don’t

  • Uppercase emails for normalization (use $toLower instead)
  • Uppercase person names for display unless that is your design choice
  • Assume $toUpper replaces case-insensitive query operators
  • Forget that null input returns null
  • Mix uppercase and lowercase normalization on the same field type

Key Takeaways

Knowledge Unlocked

Five things to remember about $toUpper

Use these points when uppercasing text in MongoDB pipelines.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $trim for whitespace cleanup, or review $toLower for lowercase normalization.

Next: $trim →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful