On this page本页内容
$binarySize
¶New in version 4.4.版本4.4中的新功能。
Returns the size of a given string or binary data value’s content in bytes.返回给定字符串或二进制数据值内容的大小(字节)。
$binarySize
has the following syntax:语法如下所示:
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 Expressions.有关表达式的详细信息,请参阅表达式。
The argument for $binarySize
must resolve to either:$binarySize
的参数必须解析为:
null
If the argument is a string or binary data value, the expression returns the size of the argument in bytes.如果参数是字符串或二进制数据值,则表达式将以字节为单位返回参数的大小。
If the argument is 如果参数为null
, the expression returns null
.null
,则表达式返回null
。
If the argument resolves to any other data type, 如果参数解析为任何其他数据类型,$binarySize
errors.$binarySize
错误。
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.$binarySize
的参数是一个字符串,则运算符计算字符串中UTF-8编码的字节数,其中每个字符可以使用1到4个字节。
For example, US-ASCII characters are encoded using one byte. 例如,US-ASCII字符使用一个字节进行编码。Characters with diacritic markings and additional Latin alphabetical characters (i.e. Latin characters outside of the English alphabet) are encoded using two bytes. 带有变音符号的字符和附加的拉丁字母字符(即英语字母表之外的拉丁字符)使用两个字节进行编码。Chinese, Japanese and Korean characters typically require three bytes, and other planes of unicode (emoji, mathematical symbols, etc.) require four bytes.中文、日文和韩文字符通常需要三个字节,而其他unicode平面(表情符号、数学符号等)则需要四个字节。
Consider the following examples:考虑下面的例子:
5 |
||
12 |
||
9 |
||
11 |
é | |
0 |
||
7 |
€ λ | |
6 |
From the 在mongo
shell, create a sample collection named images
with the following documents:mongo
shell中,使用以下文档创建名为images
的示例集合:
The following aggregation 以下聚合projects
:projects
:
name
fieldname
字段imageSize
field, which uses $binarySize
to return the size of the document’s binary
field in bytes.imageSize
字段,它使用$binarySize
返回文档binary
字段的大小(字节)。The operation returns the following result:操作返回以下结果:
The following pipeline returns the image with the largest binary data size:以下管道返回具有最大二进制数据大小的图像:
The first stage of the pipeline 管道projects
:projects
的第一阶段:
name
fieldname
字段imageSize
field, which uses binarySize
to return the size of the document’s binary
field in bytes.imageSize
字段,它使用binarySize
以字节为单位返回文档binary
字段的大小。This stage outputs the following documents to the next stage:本阶段向下一阶段输出以下文件:
The second stage 第二阶段sorts
the documents by imageSize
in descending order.sorts
按imageSize
降序排列文档。
This stage outputs the following documents to the next stage:本阶段向下一阶段输出以下文件:
The third stage 第三阶段将输出文档limits
the output documents to only return the document appearing first in the sort order:limits
为仅返回排序顺序中最先出现的文档:
See also参阅