On this page
$strcasecmp (aggregation)
在本页面
Definition
$strcasecmp
- 对两个字符串执行不区分大小写的比较。return
1 ,如果第一个字符串“大于”第二个字符串。
0 如果两个字符串相等。
如果第一个字符串“小于”第二个字符串,则为-1.
$strcasecmp具有以下语法:
{ $strcasecmp: [ <expression1>, <expression2> ] }
参数可以是任何有效的expression,只要它们解析为字符串即可。有关表达式的更多信息,请参见Expressions。
Behavior
$strcasecmp仅对于 ASCII 字符字符串具有明确定义的行为。
有关区分大小写的比较,请参见$cmp。
Example
考虑包含以下文档的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 }
以下操作使用$strcasecmp运算符对quarter
字段值与字符串"13q4"
进行不区分大小写的比较:
db.inventory.aggregate(
[
{
$project:
{
item: 1,
comparisonResult: { $strcasecmp: [ "$quarter", "13q4" ] }
}
}
]
)
该操作返回以下结果:
{ "_id" : 1, "item" : "ABC1", "comparisonResult" : -1 }
{ "_id" : 2, "item" : "ABC2", "comparisonResult" : 0 }
{ "_id" : 3, "item" : "XYZ1", "comparisonResult" : 1 }