Docs Menu

$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.

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 バイトが必要です。

次の例について考えてみます。

結果
ノート
{ $binarySize: "abcde" }

5

各文字は 1 バイトを使用してエンコードされます。

{ $binarySize: "Hello World!" }

12

各文字は 1 バイトを使用してエンコードされます。

{ $binarySize: "cafeteria" }

9

各文字は 1 バイトを使用してエンコードされます。

{ $binarySize: "cafétéria" }

11

éは 2 バイトを使用してエンコードされています。

{ $binarySize: "" }

0

空の文字列は 0 を返します。

{ $binarySize: "$€λG" }

7

は 3 バイトを使用してエンコードされています。 λは 2 バイトを使用してエンコードされています。

{ $binarySize: "寿司" }

6

各文字は 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's binary 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 }

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's binary 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 }

以下も参照してください。

項目一覧