$binarySize (aggregation)
定义
$binarySize
Returns the size of a given string or binary data value's content in bytes.
$binarySize
采用以下语法:{ $binarySize: <string or binData> } The argument can be any valid 表达式(expression) as long as it resolves to either a string or binary data value. For more information on expressions, see 表达式运算符.
行为
The argument for $binarySize
must resolve to either:
A string,
A binary data value, or
null.
If the argument is a string or binary data value, the expression returns the size of the argument in bytes.
如果参数为null
,则表达式返回null
。
If the argument resolves to any other data type,
$binarySize
errors.
String Size Calculation
If the argument for $binarySize
is a string, the operator
counts the number of UTF-8 encoded bytes in a string where each character
may use between one and four bytes.
示例,US-ASCII 字符使用一个字节进行编码。 带有变音符号的字符和其他拉丁字母字符(英语字母表之外的拉丁字符)使用两个字节进行编码。 中文、日文和韩文字符通常需要三个字节,而其他平面的 unicode(表情符号、数学符号等)需要四个字节。
以以下示例为例:
例子 | 结果 | 注意 | |
---|---|---|---|
|
| 每个字符使用一个字节进行编码。 | |
|
| 每个字符使用一个字节进行编码。 | |
|
| 每个字符使用一个字节进行编码。 | |
|
|
| |
|
| 空字符串返回 0。 | |
|
|
| |
|
| 每个字符使用三个字节进行编码。 |
例子
在 mongosh
中创建名为 images
的示例集合,其中包含以下文档:
db.images.insertMany([ { _id: 1, name: "cat.jpg", binary: new BinData(0, "OEJTfmD8twzaj/LPKLIVkA==")}, { _id: 2, name: "big_ben.jpg", binary: new BinData(0, "aGVsZmRqYWZqYmxhaGJsYXJnYWZkYXJlcTU1NDE1Z2FmZCBmZGFmZGE=")}, { _id: 3, name: "tea_set.jpg", binary: new BinData(0, "MyIRAFVEd2aImaq7zN3u/w==")}, { _id: 4, name: "concert.jpg", binary: new BinData(0, "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")}, { _id: 5, name: "empty.jpg", binary: new BinData(0, "") } ])
以下聚合projects
:
name
字段The
imageSize
field, which uses$binarySize
to return the size of the document'sbinary
field in bytes.
db.images.aggregate([ { $project: { "name": "$name", "imageSize": { $binarySize: "$binary" } } } ])
操作返回以下结果:
{ "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 } { "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 } { "_id" : 3, "name" : "teaset.jpg", "imageSize" : 16 } { "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 } { "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 }
Find Largest Binary Data
The following pipeline returns the image with the largest binary data size:
db.images.aggregate([ // First Stage { $project: { name: "$name", imageSize: { $binarySize: "$binary" } } }, // Second Stage { $sort: { "imageSize" : -1 } }, // Third Stage { $limit: 1 } ])
- 第一个阶段:
name
字段The
imageSize
field, which uses$binarySize
to return the size of the document'sbinary
field in bytes.
此阶段将以下文档输出到下一阶段:
{ "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 } { "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 } { "_id" : 3, "name" : "teaset.jpg", "imageSize" : 16 } { "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 } { "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 } - 第二阶段
第二阶段按
sorts
imageSize
文档。此阶段将以下文档输出到下一阶段:
{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 } { "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 } { "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 } { "_id" : 3, "name" : "teaset.jpg", "imageSize" : 16 } { "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 } - 第三个阶段
第三阶段
limits
输出文档,仅返回按排序顺序排在第一位的文档:{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }
另请参阅: