Definition定义
$strcasecmpPerforms case-insensitive comparison of two strings. Returns对两个字符串执行不区分大小写的比较。返回1 if first string is "greater than" the second string.如果第一个字符串“大于”第二个字符串,则为1。0 if the two strings are equal.如果两个字符串相等,则为0。-1 if the first string is "less than" the second string.如果第一个字符串“小于”第二个字符串,则为-1。
$strcasecmphas the following syntax:具有以下语法:{ $strcasecmp: [ <expression1>, <expression2> ] }The arguments can be any valid expression as long as they resolve to strings.参数可以是任何有效的表达式,只要它们解析为字符串即可。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
$strcasecmp only has a well-defined behavior for strings of ASCII characters.仅对ASCII字符串具有明确定义的行为。
For a case sensitive comparison, see 有关区分大小写的比较,请参阅$cmp.$cmp。
Example示例
Consider a 考虑使用以下文件进行inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _id: 1, item: "ABC1", quarter: "13Q1", description: "product 1" },
{ _id: 2, item: "ABC2", quarter: "13Q4", description: "product 2" },
{ _id: 3, item: "XYZ1", quarter: "14Q2", description: null }
] )
The following operation uses the 以下操作使用$strcasecmp operator to perform case-insensitive comparison of the quarter field value to the string "13q4":$strcasecmp运算符对quarter字段值与字符串"13q4"进行不区分大小写的比较:
db.inventory.aggregate(
[
{
$project:
{
item: 1,
comparisonResult: { $strcasecmp: [ "$quarter", "13q4" ] }
}
}
]
)
The operation returns the following results:该操作返回以下结果:
{ _id: 1, item: "ABC1", comparisonResult: -1 }
{ _id: 2, item: "ABC2", comparisonResult: 0 }
{ _id: 3, item: "XYZ1", comparisonResult: 1 }