$strcasecmp
The $strcasecmp operator in Amazon DocumentDB performs a case-insensitive comparison between two strings. It returns an integer value indicating the lexicographic comparison of the two input strings, ignoring case differences.
Parameters
-
string1: The first string to compare. -
string2: The second string to compare.
Example (MongoDB Shell)
This example demonstrates how to use the $strcasecmp operator to compare desk location strings in a people collection, ignoring case differences.
Create sample documents
db.people.insertMany([ { "_id": 1, "Desk": "mke233-wi" }, { "_id": 2, "Desk": "MKE233-WI" }, { "_id": 3, "Desk": "mke233-wi" } ]);
Query example
db.people.aggregate([ { $project: { item: 1, compare: { $strcasecmp: ["$Desk", "mke233-wi"] } } } ]);
Output
{ "_id" : 1, "compare" : 0 }
{ "_id" : 2, "compare" : 0 }
{ "_id" : 3, "compare" : 0 }
The output shows that the comparison between the "Desk" field and the string "mke233-wi" returns 0 for all three documents, indicating that the strings are equal when case is ignored.
Code examples
To view a code example for using the $strcasecmp command, choose the tab for the language that you want to use: