$strcasecmp (aggregation)
On this page本页内容
Definition定义
$strcasecmp
-
Performs 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。
$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.参数可以是任何有效的表达式,只要它们解析为字符串即可。有关表达式的详细信息,请参阅表达式。
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
集合:
{ "_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 }