$toLower
The $toLower operator converts a string to all lowercase letters.
📌 Syntax
{ "$toLower": <expression> }
The expression must resolve to a string.
✅ Base Example 1 – Lowercase Email
📥 Input Document
{ "email": "USER@EXAMPLE.COM" }
📌 Expression
{ "$toLower": "$email" }
📤 Output
"user@example.com"
✅ Base Example 2 – City Name Normalization
📥 Input Document
{ "city": "Toronto" }
📌 Expression
{ "$toLower": "$city" }
📤 Output
"toronto"
🧱 Ecommerce Example – Normalize Brand Name
📌 Pipeline
[
{ "$unwind": "$items" },
{
"$project": {
"brand_normalized": {
"$toLower": "$items.brand"
},
"product": "$items.name"
}
}
]
📥 Input Document
{
"items": [
{ "name": "Laptop", "brand": "HP" },
{ "name": "Keyboard", "brand": "LogiTech" }
]
}
📤 Output
[
{ "brand_normalized": "hp", "product": "Laptop" },
{ "brand_normalized": "logitech", "product": "Keyboard" }
]
🔧 Common Use Cases
- Case-insensitive comparisons
- Normalization for search, filter, and indexing
- String unification in user inputs
🔗 Related Operators
$toUpper,$trim,$concat,$substr,$toString
🧠Notes
- If the input is not a string, it will raise an error.
- Use
$ifNullor$condto handle missing values gracefully.