Menu Docs

$binarySize (aggregation)

$binarySize

Returns the size of a given string or binary data value's content in bytes.

$binarySize tem a seguinte sintaxe:

{ $binarySize: <string or binData> }

The argument can be any valid expressão as long as it resolves to either a string or binary data value. For more information on expressions, see Operadores de Expressão.

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.

Se o argumento for null, a expressão retornará 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.

Por exemplo, os caracteres US-ASCII são codificados usando um byte. Caracteres com diacríticos e caracteres alfabéticos latinos adicionais (caracteres latinos fora do alfabeto inglês) são codificados usando dois bytes. Caracteres chineses, tailandeses e coreanos normalmente exigem três bytes, e outros planos de unicode (emoji, símbolos matemáticos etc.) exigem quatro bytes.

Considere os seguintes exemplos:

Exemplo
Resultados
Notas
{ $binarySize: "abcde" }

5

Cada caractere é codificado usando um byte.

{ $binarySize: "Hello World!" }

12

Cada caractere é codificado usando um byte.

{ $binarySize: "cafeteria" }

9

Cada caractere é codificado usando um byte.

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

11

é está codificado usando dois bytes.

{ $binarySize: "" }

0

Strings vazias retornam 0.

{ $binarySize: "$€λG" }

7

é codificado usando três bytes. λ é codificado usando dois bytes.

{ $binarySize: "寿司" }

6

Cada caractere é codificado usando três bytes.

No mongosh, crie uma coleção de amostra denominada images com os seguintes documentos:

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, "") }
])

A seguinte aggregation projects:

  • O campo 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" }
}
}
])

A operação retorna o seguinte resultado:

{ "_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 }
])
Primeira etapa

O primeiro estágio do pipeline projects :

  • O campo name

  • The imageSize field, which uses $binarySize to return the size of the document's binary field in bytes.

Este estágio produz os seguintes documentos para o próximo estágio:

{ "_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 }
Segunda etapa

O segundo estágio sorts os documentos por imageSize em ordem decrescente.

Este estágio produz os seguintes documentos para o próximo estágio:

{ "_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 }
Terceiro estágio

O terceiro estágio limits os documentos de saída para retornar apenas o documento que aparece primeiro na ordem de classificação:

{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }

Veja também: