$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바이트가 필요하고, 다른 유니코드 평면(이모티콘, 수학 기호 등)에는 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 } ])
- 첫 번째 단계
파이프라인
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 } - 두 번째 단계
두 번째 단계에서는 문서를
imageSize
씩 내림차순으로
sorts
처리합니다.
이 단계에서는 다음 문서를 다음 단계로 출력합니다.
{ "_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 }