In MongoDB, the $strcasecmp
aggregation pipeline operator performs a case-insensitive comparison of two strings.
It returns either 1
, 0
, or -1
, depending on whether or not the first string is greater than, equal to, or less than the second string.
Specifically, $strcasecmp
returns:
1
if the first string is greater than the second string0
if both strings are equal-1
if the first string is less than the second string
Example
Suppose we have a collection called data
with the following documents:
{ "_id" : 1, "a" : "abc", "b" : "def" } { "_id" : 2, "a" : "abc", "b" : "abc" } { "_id" : 3, "a" : "def", "b" : "abc" } { "_id" : 4, "a" : "abc", "b" : "cba" } { "_id" : 5, "a" : "cba", "b" : "abc" }
Here’s what happens when we apply $strcasecmp
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3, 4, 5 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $strcasecmp: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : "abc", "b" : "def", "result" : -1 } { "a" : "abc", "b" : "abc", "result" : 0 } { "a" : "def", "b" : "abc", "result" : 1 } { "a" : "abc", "b" : "cba", "result" : -1 } { "a" : "cba", "b" : "abc", "result" : 1 }
Case Sensitivity
As mentioned, $strcasecmp
performs a case-insensitive comparison.
Suppose our collection contains the following document:
{ "_id" : 6, "a" : "ABC", "b" : "abc" }
The a
field contains an uppercase string, and the b
field contains the same string, but in lowercase.
Here’s what happens when we apply $strcasecmp
to both fields:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 6 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $strcasecmp: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : "ABC", "b" : "abc", "result" : 0 }
The result is 0
, which means that both strings are equal.
In other words, the comparison was case-insensitive.
Null Values
The $strcasecmp
treats two null values as equal. Also, a string is considered greater than null
.
Suppose we have the following documents in our collection:
{ "_id" : 7, "a" : "abc", "b" : null } { "_id" : 8, "a" : null, "b" : "abc" } { "_id" : 9, "a" : null, "b" : null }
Here’s what happens when we apply $strcasecmp
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 7, 8 ,9 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $strcasecmp: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : "abc", "b" : null, "result" : 1 } { "a" : null, "b" : "abc", "result" : -1 } { "a" : null, "b" : null, "result" : 0 }
Missing Fields
Missing fields have the same effect as null
.
Let’s add the following documents to our collection:
{ "_id" : 10, "a" : "abc" } { "_id" : 11, "b" : "abc" } { "_id" : 12 }
Here’s what happens when we apply $strcasecmp
to them:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 10, 11, 12 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $strcasecmp: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : "abc", "result" : 1 } { "b" : "abc", "result" : -1 } { "result" : 0 }
Other Data Types
Other data types can be compared, as long as they can resolve to a string.
Here are a bunch of documents that contain various data types:
{ "_id" : 13, "a" : 123, "b" : 456 } { "_id" : 14, "a" : 123, "b" : 123 } { "_id" : 15, "a" : 456, "b" : 123 } { "_id" : 16, "a" : NumberDecimal("123"), "b" : NumberDecimal("456") } { "_id" : 17, "a" : NumberDecimal("123"), "b" : NumberDecimal("123") } { "_id" : 18, "a" : NumberDecimal("456"), "b" : NumberDecimal("123") } { "_id" : 19, "a" : ISODate("1999-01-03T23:30:15.100Z"), "b" : "2000-01-03T23:30:15.100Z" } { "_id" : 20, "a" : ISODate("2000-01-03T23:30:15.100Z"), "b" : "2000-01-03T23:30:15.100Z" } { "_id" : 21, "a" : ISODate("2000-01-03T23:30:15.100Z"), "b" : "1999-01-03T23:30:15.100Z" }
And here’s what happens when we apply $strcasecmp
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 13, 14, 15, 16, 17, 18, 19, 20, 21 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $strcasecmp: [ "$a", "$b" ] }
}
}
]
).pretty()
Result:
{ "a" : 123, "b" : 456, "result" : -1 } { "a" : 123, "b" : 123, "result" : 0 } { "a" : 456, "b" : 123, "result" : 1 } { "a" : NumberDecimal("123"), "b" : NumberDecimal("456"), "result" : -1 } { "a" : NumberDecimal("123"), "b" : NumberDecimal("123"), "result" : 0 } { "a" : NumberDecimal("456"), "b" : NumberDecimal("123"), "result" : 1 } { "a" : ISODate("1999-01-03T23:30:15.100Z"), "b" : "2000-01-03T23:30:15.100Z", "result" : -1 } { "a" : ISODate("2000-01-03T23:30:15.100Z"), "b" : "2000-01-03T23:30:15.100Z", "result" : 0 } { "a" : ISODate("2000-01-03T23:30:15.100Z"), "b" : "1999-01-03T23:30:15.100Z", "result" : 1 }