$indexOfCP (aggregation)

On this page本页内容

Definition定义

$indexOfCP

Searches a string for an occurrence of a substring and returns the UTF-8 code point index (zero-based) of the first occurrence. 在字符串中搜索子字符串的匹配项,并返回第一个匹配项的UTF-8码位索引(从零开始)。If the substring is not found, returns -1.如果未找到子字符串,则返回-1

$indexOfCP has the following operator expression syntax:具有以下运算符表达式语法

{ $indexOfCP: [ <string expression>, <substring expression>, <start>, <end> ] }
Field字段Type类型Description描述
<string>string

Can be any valid expression as long as it resolves to a string. 可以是任何有效的表达式,只要它解析为字符串。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

If the string expression resolves to a value of null or refers to a field that is missing, $indexOfCP returns null.如果字符串表达式解析为null值或引用缺少的字段,则$indexOfCP返回null

If the string expression does not resolve to a string or null nor refers to a missing field, $indexOfCP returns an error.如果字符串表达式未解析为字符串或null,也未引用缺少的字段,则$indexOfCP将返回错误。

<substring>stringCan be any valid expression as long as it resolves to a string. 可以是任何有效的表达式,只要它解析为字符串。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式
<start>integer

Optional. 可选。An integer, or a number that can be represented as integers (such as 2.0), that specifies the starting index position for the search. 一个整数或可以表示为整数的数字(例如2.0),用于指定搜索的起始索引位置。Can be any valid expression that resolves to a non-negative integral number.可以是解析为非负整数的任何有效表达式

If unspecified, the starting index position for the search is the beginning of the string.如果未指定,则搜索的起始索引位置是字符串的开头。

<end>integer

Optional. 可选。An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. 一个整数或可以表示为整数的数字(例如2.0),用于指定搜索的结束索引位置。Can be any valid expression that resolves to a non-negative integral number. 可以是解析为非负整数的任何有效表达式If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfCP uses the <end> value as the <start> index value instead of the <end> value.如果指定<end>索引值,还应指定<start>索引值;否则,$indexOfCP<end>值用作<start>索引值,而不是<end>

If unspecified, the ending index position for the search is the end of the string.如果未指定,则搜索的结束索引位置是字符串的结尾。

Behavior行为

If the <substring expression> is found multiple times within the <string expression>, then $indexOfCP returns the index of the first <substring expression> found from the starting index position.如果在<string expression>中多次找到<substring expression>,则$indexOfCP将返回从起始索引位置找到的第一个<substring expression>的索引。

$indexOfCP returns null:

  • If <string expression> is null, or如果<string expression>为空,或
  • If <string expression> refers to a non-existing field in the input document.如果<string expression>引用输入文档中不存在的字段。

$indexOfCP returns an error:返回错误:

  • If <string expression> is not a string and not null, or如果<string expression>不是字符串且不为空,或者
  • If <substring expression> is null or is not a string or refers to a nonexistent field in the input document, or如果<substring expression>为空或不是字符串或引用输入文档中不存在的字段,或者
  • If <start> or <end> is a negative integer (or a value that can be represented as a negative integer, like -5.0).如果<start><end>是负整数(或可以表示为负整数的值,如-5.0)。

$indexOfCP returns -1:返回-1

  • If the substring is not found in the <string expression>, or如果在<string expression>中找不到子字符串,或
  • If <start> is a number greater than <end>, or如果<start>是大于<end>的数字,或
  • If <start> is a number greater than the byte length of the string.如果<start>是一个大于字符串字节长度的数字。
Example示例Results结果
{ $indexOfCP: [ "cafeteria", "e" ] }3
{ $indexOfCP: [ "cafétéria", "é" ] }3
{ $indexOfCP: [ "cafétéria", "e" ] }-1
{ $indexOfCP: [ "cafétéria", "t" ] }4
{ $indexOfCP: [ "foo.bar.fi", ".", 5 ] }7
{ $indexOfCP: [ "vanilla", "ll", 0, 2 ] }-1
{ $indexOfCP: [ "vanilla", "ll", -1 ] }Error
{ $indexOfCP: [ "vanilla", "ll", 12 ] }-1
{ $indexOfCP: [ "vanilla", "ll", 5, 2 ] }-1
{ $indexOfCP: [ "vanilla", "nilla", 3 ] }-1
{ $indexOfCP: [ null, "foo" ] }null

Examples示例

Consider an inventory collection with the following documents:考虑使用以下文档进行inventory集合:

{ "_id" : 1, "item" : "foo" }
{ "_id" : 2, "item" : "fóofoo" }
{ "_id" : 3, "item" : "the foo bar" }
{ "_id" : 4, "item" : "hello world fóo" }
{ "_id" : 5, "item" : null }
{ "_id" : 6, "amount" : 3 }

The following operation uses the $indexOfCP operator to return the code point index at which the string foo is located in each item string:以下操作使用$indexOfCP运算符返回每个项目字符串中字符串foo所在的代码点索引:

db.inventory.aggregate(
   [
     {
       $project:
          {
            cpLocation: { $indexOfCP: [ "$item", "foo" ] },
          }
      }
   ]
)

The operation returns the following results:该操作返回以下结果:

{ "_id" : 1, "cpLocation" : "0" }
{ "_id" : 2, "cpLocation" : "3" }
{ "_id" : 3, "cpLocation" : "4" }
{ "_id" : 4, "cpLocation" : "-1" }
{ "_id" : 5, "cpLocation" : null }
{ "_id" : 6, "cpLocation" : null }
Tip提示
See also: 参阅:
←  $indexOfBytes (aggregation)$integral (aggregation) →