Sample Input Documents
Suppose you have a users collection with mixed-case names:
[
{ "_id": 1, "name": "John Doe" },
{ "_id": 2, "name": "Alice Smith" },
{ "_id": 3, "name": "Bob Johnson" }
] 
The $toLower operator converts string values to lowercase in MongoDB aggregation pipelines. Use it to normalize names, emails, and tags for consistent storage, searching, and grouping.
Text to lowercase.
One expression.
Consistent text.
Transform fields.
Emails, search, UI.
Case conversion.
In MongoDB’s aggregation framework, the $toLower operator converts a string expression to lowercase. For example, "John Doe" becomes "john doe". This is useful when user-entered data arrives in mixed case and you need a consistent format for comparisons, deduplication, or display.
Unlike type-conversion operators such as $toInt or $toLong, $toLower is a string transformation operator. It does not change the BSON type — the result is still a string, just in lowercase.
Think of $toLower as MongoDB’s pipeline version of JavaScript’s .toLowerCase(). Pair it with $trim when cleaning user input that may have extra spaces.
The $toLower operator takes one string expression:
{ $toLower: <expression> } { $toLower: "Hello World" }
// Result: "hello world"
{ $toLower: "$name" }
// Lowercase the name field
{ $toLower: { $concat: [ "$first", " ", "$last" ] } }
// Lowercase a concatenated string $toLower — returns the lowercase version of the string expression.<expression> — a field path, literal string, or nested string expression.null — returns null.$project, $addFields, or $set.$toLower to normalize; use $strcasecmp to compare without changing values| Question | Answer |
|---|---|
| Operator type | Aggregation expression operator (string transform) |
| Syntax | { $toLower: <expression> } |
| Input | String or string expression |
| Output | Lowercase string (or null) |
| null input | Returns null |
| Common stages | $project, $addFields, $set |
{
$toLower: "$name"
}Lowercase a field
{
$toLower: "ABC"
}Returns "abc"
{
$toLower: "$email"
}Normalize email
{
$toLower: {
$trim: {
input: "$tag"
}
}
}Trim then lower
Normalize usernames, standardize emails, group case-insensitively, and clean tags with $toLower.
Start with a users collection and convert names to lowercase with $project.
Suppose you have a users collection with mixed-case names:
[
{ "_id": 1, "name": "John Doe" },
{ "_id": 2, "name": "Alice Smith" },
{ "_id": 3, "name": "Bob Johnson" }
] Convert each name to lowercase:
db.users.aggregate([
{
$project: {
name: 1,
lowercaseName: { $toLower: "$name" }
}
}
]) $project keeps the original name and adds a new lowercase field. The original casing is preserved if you still need it for display.
Standardize emails, group by lowercase keys, and combine with other string operators.
Emails are case-insensitive — store a lowercase version for deduplication:
db.users.aggregate([
{
$addFields: {
emailNormalized: {
$toLower: { $trim: { input: "$email" } }
}
}
}
])
// "User@Example.COM " → "user@example.com"
// "ADMIN@SITE.ORG" → "admin@site.org" Combining $trim and $toLower removes whitespace and standardizes casing. This prevents duplicate accounts like user@mail.com and User@Mail.com.
Group products by lowercase category to merge mixed-case entries:
db.products.aggregate([
{
$group: {
_id: { $toLower: "$category" },
count: { $sum: 1 },
products: { $push: "$name" }
}
},
{
$sort: { count: -1 }
}
])
// "Electronics", "ELECTRONICS", "electronics"
// → all grouped under "electronics" Using $toLower as the _id in $group merges categories that differ only by case.
Normalize both sides before comparing in $match:
db.users.aggregate([
{
$addFields: {
nameLower: { $toLower: "$name" }
}
},
{
$match: {
nameLower: "john doe"
}
},
{
$project: { name: 1 }
}
])
// Matches "John Doe", "JOHN DOE", "john doe" Convert the field to lowercase, then match against a lowercase search term. This simulates case-insensitive search inside a pipeline.
Lowercase each tag in an array with $map:
db.posts.aggregate([
{
$project: {
title: 1,
tagsNormalized: {
$map: {
input: "$tags",
as: "tag",
in: { $toLower: "$$tag" }
}
}
}
}
])
// tags: ["MongoDB", "Tutorial", "AGGREGATION"]
// → tagsNormalized: ["mongodb", "tutorial", "aggregation"] $map applies $toLower to each element in the tags array, normalizing every tag individually.
Build and lowercase a combined name field:
db.users.aggregate([
{
$project: {
fullNameLower: {
$toLower: {
$concat: [ "$firstName", " ", "$lastName" ]
}
}
}
}
])
// firstName: "John", lastName: "DOE"
// → fullNameLower: "john doe" Nest $toLower around $concat to lowercase the result of string building.
$toLower WorksThe input resolves from a field path, literal string, or nested string expression like $concat.
Uppercase and mixed-case letters become lowercase. Numbers and punctuation stay the same.
The result is stored in your pipeline field for matching, grouping, or output.
Use for search, deduplication, grouping, and consistent display.
The $toLower operator converts strings to lowercase in MongoDB aggregation pipelines. It is a simple but powerful tool for text normalization, case-insensitive grouping, and email standardization.
For uppercase conversion, use $toUpper. For case-insensitive comparison without changing values, use $strcasecmp. Next in the series: $toObjectId.
$toLower before deduplication$trim when cleaning user input$group keys for case-insensitive aggregation$map to lowercase each element in an array$toLower replaces case-insensitive query operators entirelynull input returns null$toLower with locale-aware title casing rules$toLowerUse these points when normalizing text in MongoDB pipelines.
Text transform.
PurposeOne argument.
SyntaxEmails, tags.
Use caseCase-insensitive.
PatternLower vs upper.
CompareMove on to $toObjectId for ObjectId conversion, or review $strcasecmp for case-insensitive comparison.
5 people found this page helpful