Definition定义
$indexOfBytesSearches a string for an occurrence of a substring and returns the UTF-8 byte index (zero-based) of the first occurrence. If the substring is not found, returns在字符串中搜索子字符串的出现,并返回第一个出现的UTF-8字节索引(从零开始)。如果未找到子字符串,则返回-1.-1。$indexOfByteshas the following operator expression syntax:具有以下运算符表达式语法:{ $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] }Operand操作数Description描述<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如果字符串表达式解析为nullor refers to a field that is missing,$indexOfBytesreturnsnull.null值或引用缺少的字段,$indexOfBytes将返回null。If the string expression does not resolve to a string or如果字符串表达式未解析为字符串或nullnor refers to a missing field,$indexOfBytesreturns 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,$indexOfBytesuses the<end>value as the<start>index value instead of the<end>value.<end>索引值,则还应指定<start>索引值;否则,$indexOfBytes使用<end>值作为<start>索引值,而不是<end>的值。
Behavior行为
If如果<string expression>is null,$indexOfBytesreturnsnull.<string expression>为null,则$indexOfBytes返回null。If如果对文档中不存在的字段调用$indexOfBytesis called on a field that doesn't exist in the document,$indexOfBytesreturnsnull.$indexOfBytes,$indexOfBytes将返回null。If如果<string expression>is not a string and not null,$indexOfBytesreturns an error.<string expression>不是字符串且不为null,则$indexOfBytes将返回错误。If如果<substring expression>is null,$indexOfBytesreturns an error.<substring expression>为null,则$indexOfBytes返回错误。If如果<start>or<end>is a negative number,$indexOfBytesreturns an error.<start>或<end>是负数,$indexOfBytes将返回错误。If如果<start>is a number greater than<end>,$indexOfBytesreturns-1.<start>是一个大于<end>的数字,$indexOfBytes将返回-1。If如果<start>is a number greater than the byte length of the string,$indexOfBytesreturns-1.<start>是一个大于字符串字节长度的数字,$indexOfBytes将返回-1。If如果给<start>or<end>is given a value that is not an integer,$indexOfBytesreturns an error.<start>或<end>一个不是整数的值,$indexOfBytes将返回错误。If the如果在<substring expression>is found multiple times within the<string expression>, then$indexOfBytesreturns 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 |
Examples示例
Consider an 考虑使用以下文件进行inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _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 }