On this page本页内容
$strcasecmp
Performs case-insensitive comparison of two strings. 对两个字符串执行不区分大小写的比较。
$strcasecmp has 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.有关表达式的详细信息,请参阅表达式。
$strcasecmp only has a well-defined behavior for strings of ASCII characters.对于ASCII字符字符串,只有定义良好的行为。
For a case sensitive comparison, see 有关区分大小写的比较,请参阅$cmp.$cmp。
Consider a 考虑使用以下文档的inventory collection with the following documents:inventory集合:
{ "_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 }