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
的文档名称中的字符数。