

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

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

バージョン 8.0.1 から新規。

Amazon DocumentDB の `$binarySize`演算子は、指定された文字列またはバイナリデータ値のバイト単位でサイズを返します。文字列値の場合、これは文字数ではなく、UTF-8 でエンコードされたバイト数です。マルチバイトの UTF-8 文字 (アクセント付き文字や CJK 文字など) は、文字列内の文字数より大きいバイト数を生成します。

**パラメータ**
+ `expression`: 文字列またはバイナリデータ値に解決される式。

## 例 (MongoDB シェル)
<a name="binarySize-examples"></a>

次の例は、 `$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}
]
```

## コードの例
<a name="binarySize-code"></a>

`$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()
```

------