On this page本页内容
$strLenCP
Returns the number of UTF-8 code points in the specified string.返回指定字符串中UTF-8代码点的数目。
$strLenCP has the following operator expression syntax:具有以下运算符表达式语法:
{ $strLenCP: <string expression> }
The argument can be any valid expression as long as it resolves to an string. 参数可以是任何有效的表达式,只要它解析为字符串。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If the argument resolves to a value of 如果参数解析为null or refers to a missing field, $strLenCP returns an error.null值或引用缺少的字段,$strLenCP将返回错误。
{ $strLenCP: "abcde" } | 5 |
{ $strLenCP: "Hello World!" } | 12 |
{ $strLenCP: "cafeteria" } | 9 |
{ $strLenCP: "cafétéria" } | 9 |
{ $strLenCP: "" } | 0 |
{ $strLenCP: "$€λA" } | 4 |
{ $strLenCP: "寿司" } | 2 |
The $strLenCP operator counts the number of code points in the specified string. $strLenCP运算符统计指定字符串中的代码点数。This behavior differs from the 此行为与$strLenBytes operator which counts the number of bytes in the string, where each character uses between one and four bytes.$strLenBytes运算符不同,后者计算字符串中的字节数,其中每个字符使用的字节数介于1到4个字节之间。
A collection named 名为food contains the following documents:food的集合包含以下文档:
{ "_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" : "寿司" }
The following operation uses the 以下操作使用$strLenCP operator to calculate the length of each name value:$strLenCP运算符计算每个name值的length:
db.food.aggregate(
[
{
$project: {
"name": 1,
"length": { $strLenCP: "$name" }
}
}
]
)
The operation returns the following results:该操作返回以下结果:
{ "_id" : 1, "name" : "apple", "length" : 5 }
{ "_id" : 2, "name" : "banana", "length" : 6 }
{ "_id" : 3, "name" : "éclair", "length" : 6 }
{ "_id" : 4, "name" : "hamburger", "length" : 9 }
{ "_id" : 5, "name" : "jalapeño", "length" : 8 }
{ "_id" : 6, "name" : "pizza", "length" : 5 }
{ "_id" : 7, "name" : "tacos", "length" : 5 }
{ "_id" : 8, "name" : "寿司", "length" : 2 }