$covarianceSamp (aggregation)
On this page本页内容
Definition定义
New in version 5.0. 5.0版新增。
$covarianceSamp
Returns the sample covariance of two numeric expressions that are evaluated using documents in the 返回使用$setWindowFields
stage window.$setWindowFields
阶段窗口中的文档计算的两个数值表达式的样本协方差。
$covarianceSamp
is only available in the 仅在$setWindowFields
stage.$setWindowFields
阶段中可用。
$covarianceSamp
syntax:语法:
{
$covarianceSamp: {
[
<numeric expression 1>,
<numeric expression 2>
]
}
}
Behavior行为
$covarianceSamp
behavior:行为:
Ignores non-numeric values,忽略窗口中的非数值、null
values, and missing fields in a window.null
值和缺少的字段。If the window contains one document, returns如果窗口包含一个文档,则返回null
.null
。(Compare to(与$covariancePop
, which returns0
if the window contains one document.)$covariancePop
相比,如果窗口包含一个文档,则返回0
。)If the window is empty, returns如果窗口为空,则返回null
.null
。If the window contains a如果窗口包含NaN
value, returnsNaN
.NaN
值,则返回NaN
。If the window contains one or more如果窗口包含一个或多个均为正或均为负的Infinity
value(s) that are all positive or all negative, returnsInfinity
.Infinity
值,则返回Infinity
。The returned返回的Infinity
value has the same sign as theInfinity
values in the window.Infinity
值与窗口中的Infinity
值具有相同的符号。If the window contains如果窗口包含具有不同符号的Infinity
values with different signs, returnsNaN
.Infinity
值,则返回NaN
。If the window contains a如果窗口包含decimal
value, returns adecimal
value.decimal
值,则返回一个decimal
值。If none of the previous points apply, returns a如果前面的点都不适用,则返回一个double
value.double
值。
The returned values in order of precedence are as follows:按优先级顺序返回的值如下:
NaN
Infinity
decimal
double
Example实例
Create 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 本例使用$covarianceSamp
in the $setWindowFields
stage to output the sample covariance values for the cake sales orderDate
year and quantity
values:$setWindowFields
阶段中的$covarianceSamp
来输出蛋糕销售orderDate
年份和quantity
值的样本协方差值:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
covarianceSampForState: {
$covarianceSamp: [ { $year: "$orderDate" }, "$quantity" ],
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"
partitions the documents in the collection by按state
.state
对集合中的文档进行分区。There are partitions forCA
andWA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
sorts the documents in each partition by按orderDate
in ascending order (1
), so the earliestorderDate
is first.orderDate
按升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。
output
sets the sample covariance values for theorderDate
year andquantity
values using$covarianceSamp
run in a documents window.output
使用文档窗口中运行的$covarianceSamp
设置orderDate
年份的样本协方差值和quantity
值。The window contains documents between an该unbounded
lower limit and thecurrent
document in the output.window
包含在unbounded
下限和输出中的current
文档之间的文档。This means这意味着$covarianceSamp
sets thecovarianceSampForState
field to the sample covariance values for the documents between the beginning of the partition and the current document.$covarianceSamp
将协变SampForState
字段设置为分区开始和当前文档之间文档的样本协变值。
In this output, the sample covariance is shown in the 在该输出中,样本协方差显示在covarianceSampForState
field: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 }