$strcasecmp (aggregation)

Definition

  • $strcasecmp
  • Performs case-insensitive comparison of two strings. Returns

    • 1 if first string is “greater than” the second string.
    • 0 if the two strings are equal.
    • -1 if the first string is “less than” the second string.$strcasecmp has the following syntax:
  1. { $strcasecmp: [ <expression1>, <expression2> ] }

The arguments can be any valid expression as long as they resolve to strings. Formore information on expressions, see Expressions.

Behavior

$strcasecmp only has a well-defined behavior for strings of ASCII characters.

For a case sensitive comparison, see $cmp.

Example

Consider a inventory collection with the following documents:

  1. { "_id" : 1, "item" : "ABC1", quarter: "13Q1", "description" : "product 1" }
  2. { "_id" : 2, "item" : "ABC2", quarter: "13Q4", "description" : "product 2" }
  3. { "_id" : 3, "item" : "XYZ1", quarter: "14Q2", "description" : null }

The following operation uses the $strcasecmp operator toperform case-insensitive comparison of the quarter field value tothe string "13q4":

  1. db.inventory.aggregate(
  2. [
  3. {
  4. $project:
  5. {
  6. item: 1,
  7. comparisonResult: { $strcasecmp: [ "$quarter", "13q4" ] }
  8. }
  9. }
  10. ]
  11. )

The operation returns the following results:

  1. { "_id" : 1, "item" : "ABC1", "comparisonResult" : -1 }
  2. { "_id" : 2, "item" : "ABC2", "comparisonResult" : 0 }
  3. { "_id" : 3, "item" : "XYZ1", "comparisonResult" : 1 }