$covariancePop
聚合运算符返回使用$setWindowFields
阶段窗口中的文档计算的两个数值表达式的总体协方差。
$covariancePop
运算符只能用于$setWindowFields
阶段。
语法
js
{
$covariancePop: {
[
<numeric expression 1>,
<numeric expression 2>
]
}
}
使用
$covariancePop
使用时遵守以下规则:
- 会忽略窗口中非数值、null和缺失字段的值
- 如果窗口只有一个文档,则返回
null
- 如果窗口为空,返回
null
- 如果窗口包含
NaN
则返回NaN
- 如果窗口包含一个或多个全是正无穷或全是负无穷值,返回的无穷值的符号与窗口值一致
- 如果窗口包含的无穷值符号不同,返回
NaN
- 如果窗口包含小数值,返回小数值
- 如果以上都不适用,返回双精度数
返回值的优先顺序:
- NaN
- 无穷
- 小数
- 双精度数
举例
使用下面的命令创建cakeSales
,它包含了在加利福尼亚California (CA)
和华盛顿Washington (WA)
的蛋糕销售记录:
js
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 }
] )
下面的例子在$setWindowFields
阶段使用$covariancePop
操作符输出蛋糕销售orderDate
年度和quantity
的总体协方差:
js
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
covariancePopForState: {
$covariancePop: [ { $year: "$orderDate" }, "$quantity" ],
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
在本例中:
partitionBy: "$state"
依据state
对集合文档进行分区,有两个区:CA
和WA
sortBy: { orderDate: 1 }
根据orderDate
对每个分区的文档按正序进行排序,orderDate
最早的日期排在最前面- 在文档窗口中使用
$covariancePop
输出年度orderDate
和quantity
的总体协方差
该窗口包含的文档介于无限制的下限和输出的当前文档之间,这意味着$covariancePop
将covariancePopForState
字段设置为分区开始和当前文档之间文档的总体协方差值。
在输出中,总体协方差显示在covariancePopForState
字段中:
json
{ "_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 }