$strcasecmp performs case-insensitive alphabetical comparison between two strings in the aggregation pipeline and returns a numeric value that indicates their relative order or equality.
Compares two strings without considering letter case.
Returns 0 when both strings are equal ignoring case.
Returns 1 when the first string is lexicographically greater.
Returns -1 when the first string is lexicographically smaller.
Commonly used in $project, $addFields, and $expr-based filters for flexible string matching.
Syntax
{ $strcasecmp: [ <expression1>, <expression2> ] }
<expression1>: The first string or expression to compare.
<expression2>: The second string or expression to compare.
The $strcasecmp operator resolves both expressions to strings, performs the case-insensitive comparison, and returns the result.
Examples of MongoDB $strcasecmp Operator
Examples of the $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.
Compare the value of the department field of all the documents present in employee collection with the "development" string using $strcasecmp operator.