Definition定义
New in version 5.0.在版本5.0中新增。
$covariancePop
Returns the population covariance of two numeric expressions that are evaluated using documents in the 返回使用$setWindowFields stage window.$setWindowFields阶段window中的文档计算的两个数值表达式的总体协方差。
$covariancePop is only available in the 仅在$setWindowFields stage.$setWindowFields阶段可用。
$covariancePop syntax:
{
$covariancePop:
[
<numeric expression 1>,
<numeric expression 2>
]
}Behavior行为
$covariancePop behavior:行为:
Ignores non-numeric values,忽略窗口中的非数值、nullvalues, and missing fields in a window.null值和缺失字段。If the window contains one document, returns如果窗口包含一个文档,则返回null.null。(Compare to(与$covarianceSamp, which returnsnullif the window contains one document.)$covarianceSamp相比,如果窗口包含一个文档,则返回null。)If the window is empty, returns如果窗口为空,则返回null.null。If the window contains a如果窗口包含NaNvalue, returnsNaN.NaN值,则返回NaN。If the window contains one or more如果窗口包含一个或多个全正或全负的Infinityvalue(s) that are all positive or all negative, returnsInfinity. The returnedInfinityvalue has the same sign as theInfinityvalues in the window.Infinity,则返回Infinity。返回的Infinity值与窗口中的Infinity值具有相同的符号。If the window contains如果窗口包含具有不同符号的Infinityvalues with different signs, returnsNaN.Infinity值,则返回NaN。If the window contains a如果窗口包含decimalvalue, returns adecimalvalue.decimal值,则返回decimal值。If none of the previous points apply, returns a如果前面的点都不适用,则返回一个doublevalue.double值。
The returned values in order of precedence are as follows:返回值的优先级顺序如下:
NaNInfinitydecimaldouble
Example示例
Create a 创建一个cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):cakeSales集合,其中包含加利福尼亚州(CA)和华盛顿州(WA)的蛋糕销售:
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 此示例在$covariancePop in the $setWindowFields stage to output the population covariance values for the cake sales orderDate year and quantity values:$setWindowFields阶段使用$covariancePop输出蛋糕销售orderDate的年份和quantity值的总体协方差值:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
covariancePopForState: {
$covariancePop: [ { $year: "$orderDate" }, "$quantity" ],
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"partitions the documents in the collection by按状态对集合中的文档进行分区。state. There are partitions forCAandWA.CA和WA有分区。sortBy: { orderDate: 1 }sorts the documents in each partition by按orderDatein ascending order (1), so the earliestorderDateis first.orderDate升序(1)对每个分区中的文档进行排序,因此最早的orderDate位居前列。
outputsets the population covariance values for theorderDateyear andquantityvalues using$covariancePoprun in a documents window.output使用文档窗口中的$covariancePop函数设置orderDate年份和quantity值的总体协方差值。The window contains documents between an该unboundedlower limit and thecurrentdocument in the output. This means$covariancePopsets thecovariancePopForStatefield to the population covariance values for the documents between the beginning of the partition and the current document.window包含的文档位于无限制的下限和输出中的当前文档之间。这意味着$covariancePop将covarianceOpForState字段设置为分区开始和当前文档之间的文档的总体协方差值。
In this output, the population covariance is shown in the 在此输出中,总体协方差显示在covariancePopForState field:covariancePopForState字段中:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "covariancePopForState" : 0 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "covariancePopForState" : -10.5 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "covariancePopForState" : -5.666666666666671 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "covariancePopForState" : 0 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "covariancePopForState" : -7.5 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "covariancePopForState" : 2 }