MongoDB聚合运算符:$strLenBytes

MongoDB聚合运算符:$strLenBytes


$strLenBytes聚合运算符返回指定字符串中 UTF-8 编码的字节数。

语法

{ $strLenBytes: <string expression> }

<expression>为可解析为字符串的表达式,如果解析为null或引用了不存在的字段,返回错误。

使用

$strLenBytes运算符计算字符串中 UTF-8 编码字节的数量,字符可以使用1到4个字节。例如,US-ASCII 字符使用一个字节进行编码;带有变音符号的字符和附加拉丁字母字符(即英语字母表之外的拉丁字符)使用两个字节进行编码;中文、日文和韩文字符通常需要三个字节,而其他 unicode 平面(表情符号、数学符号等)则需要四个字节。

$strLenBytes运算符与$strLenCP运算符不同,后者计算指定字符串中的代码点,而不管每个字符使用多少字节。

返回 说明
{ $strLenBytes: "abcde" } 5 每个字符使用一个字节进行编码
{ $strLenBytes: "Hello World!" } 12 每个字符使用一个字节进行编码
{ $strLenBytes: "cafeteria" } 9 每个字符使用一个字节进行编码
{ $strLenBytes: "cafétéria" } 11 é使用两个字节进行编码
{ $strLenBytes: "" } 0 空字符串返回0
{ $strLenBytes: "$€λG" } 7 使用3个字节编码,λ使用2个字节编码
{ $strLenBytes: "寿司" } 6 每个字符使用3个字符

举例

单字节和多字节字符集

使用下面的脚本创建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" : "寿司" }
 ]
)

下面的聚合使用 $strLenBytes 运算符计算名称值的长度:

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

操作返回下面的结果:

{ "_id" : 1, "name" : "apple", "length" : 5 }
{ "_id" : 2, "name" : "banana", "length" : 6 }
{ "_id" : 3, "name" : "éclair", "length" : 7 }
{ "_id" : 4, "name" : "hamburger", "length" : 9 }
{ "_id" : 5, "name" : "jalapeño", "length" : 9 }
{ "_id" : 6, "name" : "pizza", "length" : 5 }
{ "_id" : 7, "name" : "tacos", "length" : 5 }
{ "_id" : 8, "name" : "寿司", "length" : 6 }

_id: 3_id: 5 的文档均包含一个变音符号(分别为 éñ),需要两个字节进行编码。 _id: 8 的文档包含两个日语字符,每个字符使用三个字节进行编码。这使得长度大于 _id: 3_id: 5_id: 8 的文档名称中的字符数。

相关推荐

  1. MongoDB聚合运算符:$strLenBytes

    2024-05-01 07:18:05       13 阅读
  2. MongoDB聚合运算符:$add

    2024-05-01 07:18:05       37 阅读
  3. MongoDB聚合运算符:$arrayToObject

    2024-05-01 07:18:05       29 阅读
  4. MongoDB聚合运算符;$dateToParts

    2024-05-01 07:18:05       27 阅读
  5. MongoDB聚合运算符:$dayOfWeek

    2024-05-01 07:18:05       30 阅读
  6. MongoDB聚合运算符:$dayOfMonth

    2024-05-01 07:18:05       30 阅读
  7. MongoDB聚合运算符;$dateToString

    2024-05-01 07:18:05       25 阅读
  8. MongoDB聚合运算符:$dayOfYear

    2024-05-01 07:18:05       24 阅读
  9. MongoDB聚合运算符:$denseRank

    2024-05-01 07:18:05       20 阅读
  10. MongoDB聚合运算符:$dateTrunc

    2024-05-01 07:18:05       27 阅读

最近更新

  1. axios get 请求发送 FormData 数据

    2024-05-01 07:18:05       1 阅读
  2. 数据库的更新方式有哪些

    2024-05-01 07:18:05       1 阅读
  3. VSCode, 请在windows下使用git bash终端

    2024-05-01 07:18:05       1 阅读
  4. R 数据重塑

    2024-05-01 07:18:05       1 阅读
  5. 3DMAX安装失败解决办法 安装失败1603

    2024-05-01 07:18:05       1 阅读

热门阅读

  1. RCE漏洞简单总结

    2024-05-01 07:18:05       13 阅读
  2. MongoDB聚合运算符:$strLenCP

    2024-05-01 07:18:05       14 阅读
  3. Scala 重难点总结

    2024-05-01 07:18:05       15 阅读
  4. link和@import的区别

    2024-05-01 07:18:05       14 阅读
  5. iOS cocoapods 升级

    2024-05-01 07:18:05       11 阅读
  6. scala基础学习--变量,标识符,类型和类型转换

    2024-05-01 07:18:05       14 阅读
  7. redis多用户管理

    2024-05-01 07:18:05       11 阅读
  8. Docker 快速入门

    2024-05-01 07:18:05       13 阅读
  9. Oracle 存过 与Postgresql 的存过的差别

    2024-05-01 07:18:05       12 阅读
  10. 第六十七章 使用 NSD (UNIX® Linux macOS)

    2024-05-01 07:18:05       13 阅读
  11. 9、Flink 用户自定义 Functions 及 累加器详解

    2024-05-01 07:18:05       10 阅读
  12. 个人对行为型设计模式的理解 @by_TWJ

    2024-05-01 07:18:05       12 阅读
  13. Ubuntu中访问windows中的共享文件夹

    2024-05-01 07:18:05       12 阅读
  14. Vue3 + Element-plus 报错 require is not defined 处理问题

    2024-05-01 07:18:05       11 阅读