View a markdown version of this page

$sampleRate - Amazon DocumentDB

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

$sampleRate

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

Elastic 클러스터에서는 지원되지 않습니다.

Amazon DocumentDB의 $sampleRate 연산자는 지정된 속도를 기반으로 입력 문서의 무작위 샘플과 일치합니다. find() 쿼리 필터 또는 집계 $match 단계에서 사용할 수 있습니다. 선택은 확률적이므로 반환되는 문서 수는 근사치이며 실행마다 다를 수 있습니다.

파라미터

  • value: 연산자[0, 1](예: )에게 직접 전달되는 범위(포함{ $sampleRate: 0.3 })의 숫자로, 각 문서를 포함할 확률을 설정합니다. 값이 이면 문서가 0 반환되지 않고 값이 이면 모든 문서가 1 반환됩니다.

예제(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()