$covarianceSamp (aggregation)
定義
バージョン 5.0 で追加
Returns the sample covariance of two numeric expressions that are evaluated using documents in the
$setWindowFields
stage ウィンドウ.
$covarianceSamp
は$setWindowFields
ステージでのみ使用可能です。
{ $covarianceSamp: [ <numeric expression 1>, <numeric expression 2> ] }
動作
ウィンドウ内の数値以外の値、
null
値、欠落しているフィールドは無視されます。ウィンドウにドキュメントが 1 つ含まれている場合、 は
null
を返します。 (ウィンドウにドキュメントが 1 つある場合に0
を返す
$covariancePop
と比較してください。)
ウィンドウが空の場合、 は
null
を返します。ウィンドウに
NaN
の値が含まれている場合、 はNaN
を返します。ウィンドウにすべてが正またはすべてが負の
Infinity
値が 1 つ以上含まれている場合、 はInfinity
を返します。 返されるInfinity
値は、ウィンドウ内のInfinity
値と同じ符号を持ちます。ウィンドウに異なる符号を持つ
Infinity
の値が含まれている場合、 はNaN
を返します。ウィンドウに
decimal
値が含まれている場合、 はdecimal
値を返します。上記の点のいずれにも当てはまらない場合、 は
double
値を返します。
優先順位の高い順に返される値は次のとおりです。
NaN
Infinity
decimal
double
例
カリフォルニア州(CA
)とワシントン州(WA
)のケーキ販売を含む cakeSales
コレクションを作成します。
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"
はコレクション内のドキュメントをstate
でパーティショニングします。
CA
とWA
用のパーティションがあります。sortBy: { orderDate: 1 }
は、各パーティション内のドキュメントをorderDate
を基準に昇順(1
)にソートするため、最も古い
orderDate
が最初になります。
output
sets the sample covariance values for theorderDate
year andquantity
values using$covarianceSamp
run in a ドキュメント window.The ウィンドウ contains documents between an
unbounded
lower limit and thecurrent
document in the output. This means$covarianceSamp
sets thecovarianceSampForState
field to the sample covariance values for the documents between the beginning of the partition and the current document.
In this output, the sample covariance is shown in the
covarianceSampForState
field:
{ "_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 }