View a markdown version of this page

$bsonSize - Amazon DocumentDB

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

$bsonSize

バージョン 8.0.1 から新規。

Amazon DocumentDB の $bsonSize演算子は、BSON としてエンコードされたときにドキュメントのサイズをバイト単位で返します。

パラメータ

  • expression: ドキュメントまたはオブジェクトに解決される式。

例 (MongoDB シェル)

次の例は、 $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 エンコードによって異なります。

コードの例

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