MongoDB聚合运算符:$strLenCP
$strLenCP聚合运算符返回指定字符串中 UTF-8 代码点的数量。
语法
{
$strLenCP: <string expression> }
<expression>为可解析为字符串的表达式,如果解析为null或引用了不存在的字段,返回错误。
使用
$strLenCP 运算符计算指定字符串中的代码点数量,这不同于 $strLenBytes 运算符,后者计算字符串中的字节数,其中每个字符使用 1 到 4 个字节。
| 例 | 返回 |
|---|---|
{ $strLenCP: "abcde" } |
5 |
{ $strLenCP: "Hello World!" } |
12 |
{ $strLenCP: "cafeteria" } |
9 |
{ $strLenCP: "cafétéria" } |
9 |
{ $strLenCP: "" } |
0 |
{ $strLenCP: "$€λG" } |
4 |
{ $strLenCP: "寿司" } |
2 |
举例
单字节和多字节字符集
使用下面的脚本创建food集合:
db.food.insertMany(
[
{
"_id" : 1, "name" : "apple" },
{

本文介绍了MongoDB中的$strLenCP聚合运算符,用于计算UTF-8编码字符串中的代码点数量,不同于计算字节数。通过示例展示了如何在实际场景中使用它来获取字符串长度,如在处理多字节字符集时的应用。
最低0.47元/天 解锁文章
2109





