정의
버전 5.0의 신규 항목입니다.
$setWindowFields 단계 창에서 문서를 사용하여 평가된 두 숫자 표현식의 표본 공분산을 반환합니다.
$covarianceSamp is only available in the $setWindowFields stage.
$covarianceSamp 구문:
{ $covarianceSamp: [ <numeric expression 1>, <numeric expression 2> ] }
행동
$covarianceSamp 동작:
창에서 숫자가 아닌 값,
null값 및 누락된 필드를 무시합니다.창에 문서 하나가 포함되어 있으면
null을 반환합니다. (창에 문서 하나가 있는 경우0을 반환하는$covariancePop과 비교하세요.)창이 비어 있으면
null을 반환합니다.윈도우에
NaN값이 포함되어 있으면NaN을 반환합니다.창에 모두 양수 또는 모두 음수인
Infinity값이 하나 이상 포함되어 있으면Infinity반환합니다. 반환된Infinity값은 창의Infinity값과 동일한 부호를 갖습니다.창에 부호가 다른
Infinity값이 포함되어 있으면NaN을 반환합니다.창에
decimal값이 포함되어 있으면decimal값을 반환합니다.이전 점 중 어느 것도 적용되지 않으면
double값을 반환합니다.
반환되는 값은 우선 순위에 따라 다음과 같습니다.
NaNInfinitydecimaldouble
예시
캘리포니아주(CA)와 워싱턴주(WA)의 케이크 판매량이 포함된 cakeSales collection을 생성합니다.
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
This example uses $covarianceSamp in the $setWindowFields stage to output the sample covariance values for the cake sales orderDate year and quantity values:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { covarianceSampForState: { $covarianceSamp: [ { $year: "$orderDate" }, "$quantity" ], window: { documents: [ "unbounded", "current" ] } } } } } ] )
예시:
partitionBy: "$state"는 collection의 문서를state로 파티셔닝합니다.CA및WA에 대한 파티션이 있습니다.sortBy: { orderDate: 1 }각 파티션의 문서를orderDate을 기준으로 오름차순(1)으로 정렬하므로, 가장 이른orderDate이 첫 번째가 됩니다.
outputsets the sample covariance values for theorderDateyear andquantityvalues using$covarianceSamprun in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument in the output. This means$covarianceSampsets thecovarianceSampForStatefield to the sample covariance values for the documents between the beginning of the partition and the current document.
이 출력에서 표본 공분산은 covarianceSampForState 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "covarianceSampForState" : null } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "covarianceSampForState" : -21 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "covarianceSampForState" : -8.500000000000007 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "covarianceSampForState" : null } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "covarianceSampForState" : -15 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "covarianceSampForState" : 3 }