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

$strLenCP(表达式操作符)

$strLenCP

返回指定字符串中 UTF-8 代码点的数量。

$strLenCP has the following operator expression syntax:

{ $strLenCP: <string expression> }

该参数可以是解析为字符串的任何有效表达式。

If the argument resolves to a value of null or refers to a missing field, $strLenCP returns an error.

例子
结果

{ $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. This behavior differs from the $strLenBytes operator that counts the number of bytes in the string, where each character uses between one and four bytes.

创建一个 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: "寿司" }
] )

以下示例使用 $strLenCP 运算符计算每个 name 值的 length:

db.food.aggregate( [
{
$project: {
name: 1,
length: { $strLenCP: "$name" }
}
}
] )

示例输出:

[
{ _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 }
]
给本页内容打分

在此页面上