$indexOfBytes (aggregation)

On this page本页内容

Definition定义

$indexOfBytes

New in version 3.4.版本3.4中的新功能。

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

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

{ $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] }
OperandDescription描述
<string expression>

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, $indexOfBytes returns null.如果字符串表达式解析为null值或引用缺少的字段,$indexOfBytes返回null

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

<substring expression> Can be any valid expression as long as it resolves to a string. 可以是任何有效的表达式,只要它解析为字符串。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式
<start> Optional 可选。An integral number that specifies the starting index position for the search. 指定搜索起始索引位置的整数。Can be any valid expression that resolves to a non-negative integral number.可以是解析为非负整数的任何有效表达式
<end> Optional 可选。An integral number that specifies the ending index position for the search. 指定搜索结束索引位置的整数。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, $indexOfBytes uses the <end> value as the <start> index value instead of the <end> value.如果指定<end>索引值,还应指定<start>索引值;否则,$indexOfBytes<end>值用作<start>索引值,而不是<end>值。

Behavior行为

Some short examples to highlight different behavior:以下是一些简短的例子,以突出不同的行为:

Example示例Results结果
{ $indexOfBytes: [ "cafeteria", "e" ] } 3
{ $indexOfBytes: [ "cafétéria", "é" ] } 3
{ $indexOfBytes: [ "cafétéria", "e" ] } -1
{ $indexOfBytes: [ "cafétéria", "t" ] } 5
{ $indexOfBytes: [ "foo.bar.fi", ".", 5 ] } 7
{ $indexOfBytes: [ "vanilla", "ll", 0, 2 ] } -1
{ $indexOfBytes: [ "vanilla", "ll", -1 ] } -1
{ $indexOfBytes: [ "vanilla", "ll", 12 ] } -1
{ $indexOfBytes: [ "vanilla", "ll", 5, 2 ] } -1
{ $indexOfBytes: [ "vanilla", "nilla", 3 ] } -1
{ $indexOfBytes: [ 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 $indexOfBytes operator to retrieve the indexes at which the string foo is located in each item:以下操作使用$indexOfBytes运算符检索字符串foo在每个项中所处的索引:

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

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

{ "_id" : 1, "byteLocation" : "0" }
{ "_id" : 2, "byteLocation" : "4" }
{ "_id" : 3, "byteLocation" : "4" }
{ "_id" : 4, "byteLocation" : "-1" }
{ "_id" : 5, "byteLocation" : null }
{ "_id" : 6, "byteLocation" : null }

See also参阅

$indexOfCP and $indexOfArray