View a markdown version of this page

$binarySize - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$binarySize

버전 8.0.1에서 새로 추가되었습니다.

Amazon DocumentDB의 $binarySize 연산자는 지정된 문자열 또는 이진 데이터 값의 크기를 바이트 단위로 반환합니다. 문자열 값의 경우 문자 수가 아닌 UTF-8 인코딩 바이트 수입니다. 멀티바이트 UTF-8 문자(예: 액센트가 있는 문자 또는 CJK 문자)는 문자열의 문자 수보다 큰 바이트 수를 생성합니다.

파라미터

  • expression: 문자열 또는 이진 데이터 값으로 확인되는 표현식입니다.

예제(MongoDB 쉘)

다음 예제에서는 $binarySize 연산자를 사용하여 문자열 필드의 바이트 크기를 반환하는 방법을 보여줍니다.

샘플 문서 생성

db.docs.insertMany([ {_id: 1, content: "hello"}, {_id: 2, content: "Amazon DocumentDB"}, {_id: 3, content: ""} ]);

쿼리 예제

db.docs.aggregate([ { $project: { size: { $binarySize: "$content" } } } ]);

출력

[ {_id: 1, size: 5}, {_id: 2, size: 17}, {_id: 3, size: 0} ]

코드 예제

$binarySize 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('docs'); const result = await collection.aggregate([ { $project: { size: { $binarySize: "$content" } } } ]).toArray(); console.log(result); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] collection = db['docs'] result = list(collection.aggregate([ {'$project': {'size': {'$binarySize': '$content'}}} ])) print(result) finally: client.close() example()