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

$substrBytes(表达式操作符)

$substrBytes

返回字符串的子字符串。 子字符串从字符串中指定的 UTF-8 字节索引(从零开始)处的字符开始,并持续指定的字节数。

$substrBytes has the following operator expression syntax:

{ $substrBytes: [ <string expression>, <byte index>, <byte count> ] }
字段
类型
说明

string expression

字符串

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

If the argument resolves to a value of null or refers to a field that is missing, $substrBytes returns an empty string.

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

byte index

数字

指示子字符串的点。byte index可以是任何有效表达式,只要它解析为非负整数或可以表示为整数的数字(例如 2.0)。

byte index 不能引用位于多字节 UTF-8 字符中间的起始索引。

byte count

数字

可以是任何有效的表达式,只要它解析为非负整数或可以表示为整数的数字(例如 2.0)。

byte count 不能产生位于 UTF-8 字符中间的结束索引。

The $substrBytes operator uses the indexes of UTF-8 encoded bytes where each code point, or character, may use between one and four bytes to encode.

示例,US-ASCII 字符使用一个字节进行编码。 带有变音符号的字符和其他拉丁字母字符(英语字母表之外的拉丁字符)使用两个字节进行编码。 中文、日文和韩文字符通常需要三个字节,而其他平面的 unicode(表情符号、数学符号等)需要四个字节。

请务必注意string expression中的内容,因为在 UTF-8 字符中间提供byte index或byte count会导致错误。

$substrBytes differs from $substrCP in that $substrBytes counts the bytes of each character, whereas $substrCP counts the code points, or characters, regardless of how many bytes a character uses.

例子
结果
{ $substrBytes: [ "abcde", 1, 2 ] }
"bc"
{ $substrBytes: [ "Hello World!", 6, 5 ] }
"World"
{ $substrBytes: [ "cafétéria", 0, 5 ] }
"café"
{ $substrBytes: [ "cafétéria", 5, 4 ] }
"tér"
{ $substrBytes: [ "cafétéria", 7, 3 ] }

错误消息:

"Error: Invalid range, starting index is a UTF-8 continuation byte."

{ $substrBytes: [ "cafétéria", 3, 1 ] }

错误消息:

"Error: Invalid range, ending index is in the middle of a UTF-8 character."

考虑包含以下文档的 inventory 集合:

db.inventory.insertMany( [
{ _id: 1, item: "ABC1", quarter: "13Q1", description: "product 1" },
{ _id: 2, item: "ABC2", quarter: "13Q4", description: "product 2" },
{ _id: 3, item: "XYZ1", quarter: "14Q2", description: null }
] )

The following operation uses the $substrBytes operator separate the quarter value (containing only single byte US-ASCII characters) into a yearSubstring and a quarterSubstring. The quarterSubstring field represents the rest of the string from the specified byte index following the yearSubstring. It is calculated by subtracting the byte index from the length of the string using $strLenBytes.

db.inventory.aggregate(
[
{
$project: {
item: 1,
yearSubstring: { $substrBytes: [ "$quarter", 0, 2 ] },
quarterSubstring: {
$substrBytes: [
"$quarter", 2, { $subtract: [ { $strLenBytes: "$quarter" }, 2 ] }
]
}
}
}
]
)

操作返回以下结果:

{ _id: 1, item: "ABC1", yearSubstring: "13", quarterSubstring: "Q1" }
{ _id: 2, item: "ABC2", yearSubstring: "13", quarterSubstring: "Q4" }
{ _id: 3, item: "XYZ1", yearSubstring: "14", quarterSubstring: "Q2" }

使用以下文档创建 food 集合:

db.food.insertMany(
[
{ _id: 1, name: "apple" },
{ _id: 2, name: "banana" },
{ _id: 3, name: "éclair" },
{ _id: 4, name: "hamburger" },
{ _id: 5, name: "jalapeño" },
{ _id: 6, name: "pizza" },
{ _id: 7, name: "tacos" },
{ _id: 8, name: "寿司sushi" }
]
)

以下操作使用$substrBytes操作符从name值创建一个三字节的menuCode :

db.food.aggregate(
[
{
$project: {
"name": 1,
"menuCode": { $substrBytes: [ "$name", 0, 3 ] }
}
}
]
)

操作返回以下结果:

{ _id: 1, name: "apple", menuCode: "app" }
{ _id: 2, name: "banana", menuCode: "ban" }
{ _id: 3, name: "éclair", menuCode: "éc" }
{ _id: 4, name: "hamburger", menuCode: "ham" }
{ _id: 5, name: "jalapeño", menuCode: "jal" }
{ _id: 6, name: "pizza", menuCode: "piz" }
{ _id: 7, name: "tacos", menuCode: "tac" }
{ _id: 8, name: "寿司sushi", menuCode: "寿" }

提示

$substrCP

给本页内容打分

在此页面上