On this page本页内容
$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:具有以下运算符表达式语法:
Operand | |
---|---|
<string expression> |
|
<substring expression> |
|
<start> |
|
<end> |
<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> 值。 |
<string expression>
is null, $indexOfBytes
returns null
.<string expression>
为null
,$indexOfBytes
返回null
。$indexOfBytes
is called on a field that doesn’t exist in the document, $indexOfBytes
returns null
.$indexOfBytes
,$indexOfBytes
返回null
。<string expression>
is not a string and not null, $indexOfBytes
returns an error.<string expression>
不是字符串且不为null
,$indexOfBytes
返回错误。<substring expression>
is null, $indexOfBytes
returns an error.<substring expression>
为null
,$indexOfBytes
返回一个错误。<start>
or <end>
is a negative number, $indexOfBytes
returns an error.<start>
或<end>
是负数,$indexOfBytes
返回一个错误。<start>
is a number greater than <end>
, $indexOfBytes
returns -1
.<start>
是一个大于<end>
的数字,$indexOfBytes
返回-1
。<start>
is a number greater than the byte length of the string, $indexOfBytes
returns -1
.<start>
是一个大于字符串字节长度的数字,$indexOfBytes
返回-1
。<start>
or <end>
is given a value that is not an integer, $indexOfBytes
returns an error.<start>
或<end>
给定的值不是整数,$indexOfBytes
返回一个错误。<substring expression>
is found multiple times within the <string expression>
, then $indexOfBytes
returns the index of the first <substring expression>
found.<string expression>
中多次找到<substring expression>
,则$indexOfBytes
返回找到的第一个<substring expression>
的索引。Some short examples to highlight different behavior:以下是一些简短的例子,以突出不同的行为:
{ $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 |
Consider an 考虑带有以下文档的inventory
collection with the following documents:inventory
集合:
The following operation uses the 以下操作使用$indexOfBytes
operator to retrieve the indexes at which the string foo is located in each item:$indexOfBytes
运算符检索字符串foo
在每个项中所处的索引:
The operation returns the following results:操作返回以下结果:
See also参阅