Database Manual / Reference / Query Language / Expressions

$substrCP (aggregation)(聚合)

Definition定义

$substrCP

Returns the substring of a string. The substring starts with the character at the specified UTF-8 code point (CP) index (zero-based) in the string for the number of code points specified.返回字符串的子字符串。子字符串以字符串中指定的UTF-8码点(CP)索引(从零开始)处的字符开头,对应指定的码点数。

$substrCP has the following operator expression syntax:具有以下运算符表达式语法

{ $substrCP: [ <string expression>, <code point index>, <code point count> ] }
Field字段Type类型Description描述
string expressionstring字符串

The string from which the substring will be extracted. 从中提取子字符串的字符串。string expression can be any valid expression as long as it resolves to a string. string expression可以是任何有效的表达式,只要它解析为字符串即可。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

If the argument resolves to a value of null or refers to a field that is missing, $substrCP returns an empty string.如果参数解析为null值或引用缺少的字段,$strCP将返回一个空字符串。

If the argument does not resolve to a string or null nor refers to a missing field, $substrCP returns an error.如果参数未解析为字符串或null,也未引用缺失的字段,$subrCP将返回错误。

code point indexnumber数字Indicates the starting point of the substring. code point index can be any valid expression as long as it resolves to a non-negative integer.指示子字符串的起点。code point index可以是任何有效的表达式,只要它解析为非负整数即可。
code point countnumber数字Can be any valid expression as long as it resolves to a non-negative integer or number that can be represented as an integer (such as 2.0).可以是任何有效的表达式,只要它解析为非负整数或可以表示为整数的数字(如2.0)。
Example示例Results结果
{ $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 ] }"é"

Behavior行为

The $substrCP operator uses the code points to extract the substring. This behavior differs from the $substrBytes operator which extracts the substring by the number of bytes, where each character uses between one and four bytes.$strCP运算符使用代码点来提取子字符串。此行为与$strBytes运算符不同,后者按字节数提取子字符串,其中每个字符使用一到四个字节。

Example示例

Single-Byte Character Set单字节字符集

Consider an inventory collection with the following documents:考虑使用以下文件进行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 $substrCP operator to separate the quarter value into a yearSubstring and a quarterSubstring. 以下操作使用$subrCP运算符将quarter值分为yearSubstringquarterSubstringThe quarterSubstring field represents the rest of the string from the specified byte index following the yearSubstring. quarterSubstring字段表示yearSubstring之后指定字节索引中字符串的其余部分。It is calculated by subtracting the byte index from the length of the string using $strLenCP.它是通过使用$strLenCP从字符串的长度中减去byte index索引来计算的。

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

The operation returns the following results:该操作返回以下结果:

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

Single-Byte and Multibyte Character Set单字节和多字节字符集

Create a food collection with the following documents:使用以下文档创建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" }
] )

The following example uses the $substrCP operator to create a three byte menuCode from the name value:以下示例使用$subrCP运算符从name值创建一个三字节的menuCode

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

The operation returns the following results:该操作返回以下结果:

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