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