$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 式 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 文字は 1 バイトを使用してエンコードされます。 発音区別符号を持つ文字と追加のラテン文字(アルファベットの外のラテン文字)を持つ文字は、2 バイトを使用してエンコードされます。 中国語、日本語、 韓国語の文字は通常 3 バイト必要であり、Unicode の他のプレーン(文字列、数学記号など)には 4 バイトが必要です。
次の例について考えてみます。
例 | 結果 | ノート | |
---|---|---|---|
|
| 各文字は 1 バイトを使用してエンコードされます。 | |
|
| 各文字は 1 バイトを使用してエンコードされます。 | |
|
| 各文字は 1 バイトを使用してエンコードされます。 | |
|
|
| |
|
| 空の文字列は 0 を返します。 | |
|
|
| |
|
| 各文字は 3 バイトを使用してエンコードされます。 |
例
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 } ])
- 第 1 ステージ
パイプラインの第一ステージ
projects
: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 } - 第 2 ステージ
第二ステージでは、
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 } - 第 3 ステージ
第三ステージ
limits
では、出力ドキュメントをソート順で最初に表示されるドキュメントのみ返します。{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }
以下も参照してください。