$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。
  • Returns 0 if the two strings are equal.如果两个字符串相等,则返回0。
  • Returns -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 }
←  $stdDevSamp (aggregation)$strLenBytes (aggregation) →