文档菜单
文档首页
/
MongoDB 手册
/ / /

$substrBytes (聚合)

本页内容

  • 定义
  • 行为
  • 示例
$substrBytes

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

$substrBytes具有以下运算符表达式语法:

{ $substrBytes: [ <string expression>, <byte index>, <byte count> ] }
字段
类型
描述
字符串表达式
字符串

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

如果参数解析为null或引用缺失的字段,$substrBytes返回空字符串。

如果参数没有解析为字符串或null,也不引用缺失的字段,$substrBytes返回错误。

字节索引
数字

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

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

字节计数
数字

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

字节计数不能导致结束索引位于UTF-8字符中间。

操作符 $substrBytes 使用 UTF-8 编码的字节索引,其中每个代码点或字符可能使用 1 到 4 个字节进行编码。

例如,US-ASCII 字符使用 1 个字节进行编码。带有重音符号的字符和额外的拉丁字母字符(英语字母表之外的拉丁字符)使用 2 个字节进行编码。中文、日文和韩文字符通常需要 3 个字节,而 Unicode 的其他平面(表情符号、数学符号等)需要 4 个字节。

注意 字符串表达式 中的内容非常重要,因为如果在 UTF-8 字符的中间提供一个 字节索引字节计数,将导致错误。

$substrBytes$substrCP 不同,因为 $substrBytes 计算每个字符的字节,而 $substrCP 计算代码点或字符,而不考虑字符使用的字节数。

示例
结果
{ $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 ] }

带错误消息的错误

"错误:无效的范围,起始索引是 UTF-8 续字节。"

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

带错误消息的错误

"错误:无效的范围,结束索引位于 UTF-8 字符的中间。"

考虑以下文档的库存集合

{ "_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 }

以下操作使用 $substrBytes 操作符将只包含单字节 US-ASCII 字符的 季度 值分别分离到 yearSubstringquarterSubstringquarterSubstring 字段代表从 yearSubstring 后的指定 字节索引 开始的字符串的其余部分。它是通过使用 $strLenBytes 从字符串长度中减去 字节索引 来计算的。

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

操作返回以下结果

{ "_id" : 1, "item" : "ABC1", "yearSubstring" : "13", "quarterSubtring" : "Q1" }
{ "_id" : 2, "item" : "ABC2", "yearSubstring" : "13", "quarterSubtring" : "Q4" }
{ "_id" : 3, "item" : "XYZ1", "yearSubstring" : "14", "quarterSubtring" : "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" : "寿" }

提示

另请参阅

返回

子串

本页内容