MongoDB $strcasecmp Operator
Last Updated :
27 Jun, 2024
$strcasecmp operator in MongoDB is a string expression operator used to perform case-insensitive comparisons between two strings. It is commonly utilized within the aggregation pipeline to facilitate these comparisons.
In this article, We will learn about the MongoDB $strcasecmp Operator by understanding various examples.
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 this operator can be explained in three cases:
- 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> ] }
Here, the arguments passed in this operator can be any valid expression until they resolve to strings.
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.
Output

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:

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:

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.
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.
FAQs on MongoDB $strcasecmp
What is the purpose of the $strcasecmp operator in MongoDB?
The $strcasecmp operator is used to perform case-insensitive comparisons between two strings in MongoDB, typically within the aggregation pipeline.
What does the $strcasecmp operator return when the two strings are equal?
When the two strings are equal, the $strcasecmp operator returns 0.
Can the $strcasecmp operator be used with embedded documents?
Yes, the $strcasecmp operator can be used to compare strings within embedded documents in MongoDB.
Is the $strcasecmp operator applicable to non-Roman alphabet glyphs?
The $strcasecmp operator may not make sense when applied to glyphs outside the Roman alphabet, as it internally capitalizes strings before comparing them to provide a case-insensitive comparison