翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$sampleRate
バージョン 8.0.1 から新規。
Elastic クラスターではサポートされていません。
Amazon DocumentDB の$sampleRate演算子は、指定されたレートに基づいて入力ドキュメントのランダムなサンプルに一致します。find() クエリフィルターまたは集約$matchステージで使用できます。選択は確率的であるため、返されるドキュメントの数は概算であり、実行ごとに異なる場合があります。
パラメータ
例 (MongoDB シェル)
次の例では$sampleRate、 を使用して、コレクション内のドキュメントの約 30% を返します。
サンプルドキュメントを作成する
db.events.insertMany([
{ _id: 1, type: "click" },
{ _id: 2, type: "view" },
{ _id: 3, type: "click" },
{ _id: 4, type: "view" },
{ _id: 5, type: "purchase" }
]);
クエリの例
db.events.aggregate([
{ $match: { $sampleRate: 0.3 } }
]);
出力
オペレーションはドキュメントのランダムなサブセットを返します。$sampleRate は確率的であるため、返される特定のドキュメントとその数はクエリが実行されるたびに異なります。
コードの例
$sampleRate コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false';
const client = new MongoClient(uri);
try {
await client.connect();
const db = client.db('test');
const collection = db.collection('events');
// Return approximately 30% of the documents
const result = await collection.aggregate([
{ $match: { $sampleRate: 0.3 } }
]).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['events']
# Return approximately 30% of the documents
result = list(collection.aggregate([
{ '$match': { '$sampleRate': 0.3 } }
]))
print(result)
finally:
client.close()
example()