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"

最低0.47元/天 解锁文章
905

被折叠的 条评论
为什么被折叠?



