

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

# $bsonSize
<a name="bsonSize"></a>

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

Amazon DocumentDB의 `$bsonSize` 연산자는 BSON으로 인코딩될 때 문서의 크기를 바이트 단위로 반환합니다.

**파라미터**
+ `expression`: 문서 또는 객체로 확인되는 표현식입니다.

## 예제(MongoDB 쉘)
<a name="bsonSize-examples"></a>

다음 예제에서는 `$bsonSize` 연산자를 사용하여 각 문서의 BSON 크기를 반환하는 방법을 보여줍니다.

**샘플 문서 생성**

```
db.items.insertMany([
  {_id: 1, name: "a", tags: ["x"]},
  {_id: 2, name: "ab", tags: ["x", "y", "z"]},
  {_id: 3, name: "abc", tags: []}
]);
```

**쿼리 예제**

```
db.items.aggregate([
  { $project: { docSize: { $bsonSize: "$$ROOT" } } }
]);
```

**출력**

```
[
  {_id: 1, docSize: 46},
  {_id: 2, docSize: 65},
  {_id: 3, docSize: 39}
]
```

**참고**  
정확한 크기는 문서 필드의 BSON 인코딩에 따라 달라집니다.

## 코드 예제
<a name="bsonSize-code"></a>

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

------
#### [ 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('items');
    const result = await collection.aggregate([
      { $project: { docSize: { $bsonSize: "$$ROOT" } } }
    ]).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['items']
        result = list(collection.aggregate([
            {'$project': {'docSize': {'$bsonSize': '$$ROOT'}}}
        ]))
        print(result)
    finally:
        client.close()

example()
```

------