On this page本页内容
New in version 5.0.在版本5.0中新增。
Returns the population covariance of two numeric expressions that are evaluated using documents in the 返回使用$setWindowFields stage window.$setWindowFields阶段窗口中的文档计算的两个数值表达式的总体协方差。
$covariancePop is only available in the 仅在$setWindowFields stage.$setWindowFields阶段可用。
$covariancePop syntax:语法:
{
$covariancePop: {
[
<numeric expression 1>,
<numeric expression 2>
]
}
}
$covariancePop behavior:行为:
null values, and missing fields in a window.null值和缺少的字段。null. null。$covarianceSamp, which returns null if the window contains one document.)$covarianceSamp相比,如果窗口包含一个文档,则返回null。)null.null。NaN value, returns NaN.NaN值,则返回NaN。Infinity value(s) that are all positive or all negative, returns Infinity. Infinity值,则返回Infinity。Infinity value has the same sign as the Infinity values in the window.Infinity与窗口中的Infinity具有相同的符号。Infinity values with different signs, returns NaN.Infinity值,则返回NaN。decimal value, returns a decimal value.decimal值,则返回decimal值。double value.double值。The returned values in order of precedence are as follows:返回值的优先顺序如下:
NaNInfinitydecimaldoubleCreate a 创建包含加利福尼亚州(cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):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 本示例使用$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" state. state对集合中的文档进行分区。CA and WA.CA和WA的分区。sortBy: { orderDate: 1 } orderDate in ascending order (1), so the earliest orderDate is first.orderDate按升序(1)对每个分区中的文档进行排序,因此最早的orderDate是第一个。output sets the population covariance values for the 使用文档窗口中运行的orderDate year and quantity values using $covariancePop run in a documents window.$covariancePop设置orderDate年份和数量值的总体协方差值。
The window contains documents between an 该窗口包含的文档位于unbounded lower limit and the current document in the output. unbounded下限和输出中的current文档之间。This means 这意味着$covariancePop sets the covariancePopForState field to the population covariance values for the documents between the beginning of the partition and the current document.$covariancePop将covariancePopForState字段设置为分区开始和当前文档之间文档的总体协方差值。
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 }