The $strcasecmp operator compares two strings without regard to letter case. It returns 0 when they match, -1 when the first is less, and 1 when the first is greater — ideal for case-insensitive filtering and validation.
01
Case-Insensitive
Ignore A vs a.
02
Return Values
0, -1, or 1.
03
Syntax
[ string1, string2 ].
04
$expr Queries
Filter in find().
05
Use Cases
Roles, status, emails.
06
vs $eq
Case-sensitive compare.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $strcasecmp operator performs a case-insensitive comparison of two strings. For example, { $strcasecmp: [ "Active", "active" ] } returns 0 because the strings are equal when case is ignored.
Use it when user input or stored data may vary in capitalization — such as status fields ("Pending" vs "pending"), role names, or email local parts. Pair it with $eq to test equality, or with $cond for conditional logic.
💡
Beginner Tip
Think of $strcasecmp like JavaScript’s localeCompare with case ignored, but it returns numeric 0, -1, or 1 instead of a boolean.
Foundation
📝 Syntax
The $strcasecmp operator takes an array of two string expressions:
mongosh
{ $strcasecmp: [ <string1>, <string2> ] }
Return Values
mongosh
{ $strcasecmp: [ "Active", "active" ] }
// Result: 0 (equal, case ignored)
{ $strcasecmp: [ "apple", "banana" ] }
// Result: -1 (apple comes before banana)
{ $strcasecmp: [ "zebra", "apple" ] }
// Result: 1 (zebra comes after apple)
Syntax Rules
First argument — string expression (field path or literal).
Second argument — string expression to compare against.
Returns 0 — strings are equal (case-insensitive).
Returns -1 — first string is lexicographically less.
Returns 1 — first string is lexicographically greater.
Use inside $project, $addFields, $match with $expr, or find queries with $expr.
Both arguments can be field paths. Useful for validating that a confirmation field matches the original, even if the user typed different casing.
Applications
🚀 Use Cases
Status and role filtering — match "active", "admin", or other enums regardless of stored casing.
User input validation — compare form submissions to stored values without forcing lowercase on every write.
Duplicate detection — find records where two string fields should match case-insensitively.
Reporting flags — add computed booleans like isActive or isPremium in pipelines.
🧠 How $strcasecmp Works
1
MongoDB reads both strings
Each argument resolves to a string — from field paths, literals, or nested expressions.
Input
2
Case is normalized for comparison
MongoDB compares the strings as if uppercase and lowercase letters were equivalent.
Compare
3
A numeric result is returned
0 for equal, -1 if the first is less, 1 if the first is greater.
Output
=
📝
Case-insensitive logic
Wrap in $eq ... 0 for equality, or use the number for ordering.
Wrap Up
Conclusion
The $strcasecmp operator compares strings without regard to letter case and returns 0, -1, or 1. It is essential for case-insensitive filtering, validation, and conditional logic in aggregation pipelines and $expr queries.
For equality checks, always pair it with { $eq: [ { $strcasecmp: [...] }, 0 ] }. Next in the series: $strLenBytes.
Compare two fields when validating confirmation inputs
Store canonical lowercase values if you need indexed exact-match queries without $expr
❌ Don’t
Expect a boolean — $strcasecmp returns a number, not true/false
Confuse it with $eq, which is case-sensitive for strings
Assume it trims whitespace — leading or trailing spaces still matter
Rely on it for locale-specific sorting (accents, Unicode rules)
Forget that null inputs propagate as null
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $strcasecmp
Use these points when comparing strings in MongoDB.
5
Core concepts
📝01
Ignore Case
Aa = aa.
Purpose
🔢02
0 / -1 / 1
Numeric result.
Returns
📝03
[ s1, s2 ]
Two strings.
Syntax
🛠04
$eq ... 0
Equality test.
Pattern
⚠05
vs $eq
Case-sensitive.
Compare
❓ Frequently Asked Questions
$strcasecmp compares two strings without regard to letter case. It returns 0 when the strings match, -1 when the first string is less than the second, and 1 when the first is greater. Use it for case-insensitive equality checks and sorting logic.
The syntax is { $strcasecmp: [ <string1>, <string2> ] }. Both arguments can be field references like "$status" or literal strings like "active".
Compare the result to zero: { $eq: [ { $strcasecmp: [ "$field", "target" ] }, 0 ] }. This returns true when the strings match case-insensitively.
Normal comparison with $eq is case-sensitive: "Active" and "active" are not equal. $strcasecmp ignores case, so those two strings compare as equal (returns 0).
Yes. Wrap it inside $expr in a find filter or use it in aggregation $match stages. Example: { $expr: { $eq: [ { $strcasecmp: [ "$role", "admin" ] }, 0 ] } }.