MongoDB $strcasecmp Operator
MongoDB, one of the leading NoSQL databases, provides a powerful suite of operators for manipulating and comparing data. Among these operators, the $strcasecmp operator plays a crucial role in performing case-insensitive string comparisons. Whether we’re comparing user inputs, matching field values, or ensuring that your queries are flexible, $strcasecmp offers an efficient solution within the aggregation pipeline.
MongoDB $strcasecmp Operator
The $strcasecmp operator in MongoDB is used to compare two strings. It is a string expression operator used in the aggregation pipeline to perform case-insensitive string comparisons. The result of the $strcasecmp operator is based on the lexicographical (alphabetical) comparison of the strings, and it can return one of the following three values:
- If the first string is greater than the second string, then this operator will return 1.
- If the first string is less than the second string, then this operator will return -1.
- If both the strings are equal, then this operator will return 0.
Syntax
{ $strcasecmp: [ <expression1>, <expression2> ] }
Key Terms
<expression1>: The first string or expression to compare.<expression2>: The second string or expression to compare.- The
$strcasecmpoperator resolves both expressions to strings, performs the case-insensitive comparison, and returns the result.
When to Use the $strcasecmp Operator?
The MongoDB $strcasecmp operator is particularly useful when:
- Case-insensitive comparisons are required between strings (e.g., user input or field values).
- We need to ignore case sensitivity during string comparisons in queries.
- We want to sort, filter, or manipulate data based on case-insensitive string matching.
- It is widely used in scenarios such as comparing names, departments, email addresses, and other string fields where case variation is irrelevant.
Examples of MongoDB $strcasecmp Operator
To understand it better, let's look at some examples of the MongoDB $strcasecmp Operator. In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: employee
- Document: three documents that contain the details of the employees in the form of field-value pairs.

Example 1: Using $strcasecmp Operator
In this example, we are going compare the value of the department field of all the documents present in employee collection with the "development" string using $strcasecmp operator.
Query:
db.employee.aggregate([
... {$project: {"name.first": 1, _id: 0, result:
... {$strcasecmp: ["$department", "development"]}}}])
Output:

Explanation:
- The query compares the
departmentfield of each document with the string"development". - The result shows
0if they are equal (ignoring case),1if the department name is lexicographically greater than"development", and-1if it is smaller.
Example 2: Using $strcasecmp Operator in the Embedded Document
In this example, we are going compare the value of the name.first field of all the documents present in employee collection with the "Sunita" string using $strcasecmp operator.
Query:
db.employee.aggregate([
... {$project: {result: {$strcasecmp: ["$name.first", "Sunita"]}}}])
Output:

Explanation:
- The query compares the
name.firstfield with the string"Sunita". - The operator returns
0if the names are equal (ignoring case),1if the first string is greater, and-1if it is smaller.
KeyTakeAways About MongoDB strcasecmp Operator
- The MongoDB $strcasecmp operator is used in the aggregation pipeline stages to perform a case-insensitive comparison of two strings.
- It returns 1 if the first string is "greater than" the second string, 0 if the two strings are equal, and -1 if the first string is "less than" the second string.
- The operator is useful for scenarios where case sensitivity is not required, and a simple string comparison is needed.
- $strcasecmp internally capitalizes strings before comparing them to provide a case-insensitive comparison.
- It may not make sense when applied to glyphs outside the Roman alphabet.
Common Scenarios for Using $strcasecmp
- User Input Validation: When users input data such as names, email addresses, or other string fields,
$strcasecmpcan be used to ensure case-insensitive matching. - Sorting Data: You can use
$strcasecmpin sorting operations to perform case-insensitive sorting on fields likenameoremail. - Filtering Records: Use
$strcasecmpto filter records based on a case-insensitive comparison, such as finding all employees in a certain department regardless of case. - Data Deduplication: When cleaning or processing data,
$strcasecmpcan help identify duplicates that differ only in case.
Conclusion
The MongoDB $strcasecmp operator is a valuable tool for performing case-insensitive string comparisons within the aggregation pipeline. By returning 1, -1, or 0 based on the comparison results, it allows for flexible and efficient string comparisons. Whether comparing field values in simple or embedded documents, the $strcasecmp operator helps developers handle case-insensitive data scenarios effectively. By mastering the $strcasecmp operator, we can enhance our MongoDB queries, ensure more robust matching, and make your application logic more reliable in handling string data.