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

$substrCP(聚合)

本页内容

  • 定义
  • 行为
  • 示例
$substrCP

返回字符串的子字符串。子字符串从字符串中指定UTF-8码点(CP)索引(基于0)的字符开始,该索引指定了码点的数量。

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

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

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

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

如果参数未解析为字符串、null或引用一个缺失的字段,则$substrCP返回错误。

码点索引
数字
指示子字符串的起始点。 code point index 可以是任何有效的 表达式,只要它解析为非负整数。
code point count
数字
可以是任何有效的 表达式,只要它解析为非负整数或可以表示为整数的数字(例如 2.0)。
示例
结果
{ $substrCP: [ "abcde", 1, 2 ] }
"bc"
{ $substrCP: [ "Hello World!", 6, 5 ] }
"World"
{ $substrCP: [ "cafétéria", 0, 5 ] }
"cafét"
{ $substrCP: [ "cafétéria", 5, 4 ] }
"éria"
{ $substrCP: [ "cafétéria", 7, 3 ] }
"ia"
{ $substrCP: [ "cafétéria", 3, 1 ] }
"é"

$substrCP运算符使用code points提取子字符串。这种行为与$substrBytes运算符不同,后者通过字节数提取子字符串,其中每个字符使用1到4个字节。

考虑以下文档的库存集合

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

以下操作使用$substrCP运算符将季度值分离成yearSubstringquarterSubstring。其中quarterSubstring字段表示从yearSubstring之后的指定字节索引的字符串的其余部分。它通过使用$strLenCP从字符串长度减去字节索引来计算。

db.inventory.aggregate(
[
{
$project: {
item: 1,
yearSubstring: { $substrCP: [ "$quarter", 0, 2 ] },
quarterSubtring: {
$substrCP: [
"$quarter", 2, { $subtract: [ { $strLenCP: "$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" }
]
)

以下示例使用$substrCP运算符从name值创建一个三字节的menuCode

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

该操作返回以下结果

{ "_id" : 1, "name" : "apple", "menuCode" : "app" }
{ "_id" : 2, "name" : "banana", "menuCode" : "ban" }
{ "_id" : 3, "name" : "éclair", "menuCode" : "écl" }
{ "_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" : "寿司s" }

提示

另请参阅

后退

$substrBytes

本页内容