View a markdown version of this page

$covariancePop - Amazon DocumentDB

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

$covariancePop

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

Amazon DocumentDB의 $covariancePop 연산자는 두 숫자 표현식의 모집단 공분산을 반환합니다. $setWindowFields 단계에서만 사용되는 창 연산자이며 $group 또는 $bucket 단계에서는 유효하지 않습니다. 단계의 output 필드 아래에 연산자를 지정하고 선택적으로 window 문서로 창 경계를 정의합니다.

파라미터

  • $covariancePop는 2개 요소 배열을 사용합니다. [ <expression1>, <expression2> ]여기서 각 요소는 숫자 표현식("$x", 표현식 또는 상수와 같은 필드 경로)입니다. 창의 문서에 걸쳐 두 표현식의 모집단 공분산을 반환합니다.

예제(MongoDB 쉘)

다음 예제에서는 문서를 기준으로 분할하고series, 각 파티션을 기준으로 정렬하고t, 전체 파티션에 y 대한 x 및의 모집단 공분산을 계산합니다.

샘플 문서 생성

db.measurements.insertMany([ { _id: 1, series: "A", t: 1, x: 1, y: 2 }, { _id: 2, series: "A", t: 2, x: 2, y: 5 }, { _id: 3, series: "A", t: 3, x: 3, y: 8 }, { _id: 4, series: "B", t: 1, x: 1, y: 8 }, { _id: 5, series: "B", t: 2, x: 2, y: 5 }, { _id: 6, series: "B", t: 3, x: 3, y: 2 } ]);

쿼리 예제

db.measurements.aggregate([ { $setWindowFields: { partitionBy: "$series", sortBy: { t: 1 }, output: { covariance: { $covariancePop: [ "$x", "$y" ], window: { documents: ["unbounded", "unbounded"] } } } } } ]);

출력

[ { "_id": 1, "series": "A", "t": 1, "x": 1, "y": 2, "covariance": 2 }, { "_id": 2, "series": "A", "t": 2, "x": 2, "y": 5, "covariance": 2 }, { "_id": 3, "series": "A", "t": 3, "x": 3, "y": 8, "covariance": 2 }, { "_id": 4, "series": "B", "t": 1, "x": 1, "y": 8, "covariance": -2 }, { "_id": 5, "series": "B", "t": 2, "x": 2, "y": 5, "covariance": -2 }, { "_id": 6, "series": "B", "t": 3, "x": 3, "y": 2, "covariance": -2 } ]

파티션의 모든 문서는 시리즈(x x 및 y가 함께 증가) 및 시리즈A(x가 증가2하면 By가 감소)와 같이 동일한 covariance, 즉 해당 파티션의 y 모든 문서에 -2 대한 및의 모집단 공분산을 수신합니다.

코드 예제

$setWindowFields 단계에서 $covariancePop 연산자를 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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 measurements = db.collection('measurements'); await measurements.insertMany([ { _id: 1, series: "A", t: 1, x: 1, y: 2 }, { _id: 2, series: "A", t: 2, x: 2, y: 5 }, { _id: 3, series: "A", t: 3, x: 3, y: 8 }, { _id: 4, series: "B", t: 1, x: 1, y: 8 }, { _id: 5, series: "B", t: 2, x: 2, y: 5 }, { _id: 6, series: "B", t: 3, x: 3, y: 2 } ]); const result = await measurements.aggregate([ { $setWindowFields: { partitionBy: "$series", sortBy: { t: 1 }, output: { covariance: { $covariancePop: [ "$x", "$y" ], window: { documents: ["unbounded", "unbounded"] } } } } } ]).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'] measurements = db['measurements'] measurements.insert_many([ { '_id': 1, 'series': 'A', 't': 1, 'x': 1, 'y': 2 }, { '_id': 2, 'series': 'A', 't': 2, 'x': 2, 'y': 5 }, { '_id': 3, 'series': 'A', 't': 3, 'x': 3, 'y': 8 }, { '_id': 4, 'series': 'B', 't': 1, 'x': 1, 'y': 8 }, { '_id': 5, 'series': 'B', 't': 2, 'x': 2, 'y': 5 }, { '_id': 6, 'series': 'B', 't': 3, 'x': 3, 'y': 2 } ]) result = list(measurements.aggregate([ { '$setWindowFields': { 'partitionBy': '$series', 'sortBy': { 't': 1 }, 'output': { 'covariance': { '$covariancePop': [ '$x', '$y' ], 'window': { 'documents': ['unbounded', 'unbounded'] } } } } } ])) print(result) finally: client.close() example()