对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$indexOfBytes(表达式操作符)

$indexOfBytes

在字符串中搜索子string的出现位置,并返回第一次出现时的 UTF-8 字节索引(从零开始)。 如果未找到子字符串,则返回-1 。

$indexOfBytes has the following operator expression syntax:

{ $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] }
操作数
说明

<string expression>

可以是任何有效的表达式,只要它解析为字符串即可。 有关表达式的更多信息,请参阅表达式。

If the string expression resolves to a value of null or refers to a field that is missing, $indexOfBytes returns null.

If the string expression does not resolve to a string or null nor refers to a missing field, $indexOfBytes returns an error.

<substring expression>

可以是任何有效的表达式,只要它解析为字符串即可。 有关表达式的更多信息,请参阅表达式。

<start>

可选一个整数,用于指定Atlas Search的起始索引位置。 可以是解析为非负整数的任何有效表达式。

<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.

  • If <string expression> is null, $indexOfBytes returns null.

  • If $indexOfBytes is called on a field that doesn't exist in the document, $indexOfBytes returns null.

  • If <string expression> is not a string and not null, $indexOfBytes returns an error.

  • If <substring expression> is null, $indexOfBytes returns an error.

  • If <start> or <end> is a negative number, $indexOfBytes returns an error.

  • If <start> is a number greater than <end>, $indexOfBytes returns -1.

  • If <start> is a number greater than the byte length of the string, $indexOfBytes returns -1.

  • If <start> or <end> is given a value that is not an integer, $indexOfBytes returns an error.

  • If the <substring expression> is found multiple times within the <string expression>, then $indexOfBytes returns the index of the first <substring expression> found.

一些突出不同行为的简短示例:

例子
结果

{ $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

考虑包含以下文档的 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:

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

操作返回以下结果:

{ _id: 1, byteLocation: "0" }
{ _id: 2, byteLocation: "4" }
{ _id: 3, byteLocation: "4" }
{ _id: 4, byteLocation: "-1" }
{ _id: 5, byteLocation: null }
{ _id: 6, byteLocation: null }
给本页内容打分

在此页面上