对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$covariancePop(表达式操作符)

版本 5.0 中的新增功能。

$covariancePop

返回使用$setWindowFields 阶段 窗口 中的文档对这两个数值 表达式 进行评估的两个数值表达式的总体协方差。

$covariancePop 只能在 $setWindowFields 阶段使用。

$covariancePop 事务语法:

{ $covariancePop: [ <numeric expression 1>, <numeric expression 2> ]}

$covariancePop 行为:

  • 忽略窗口中的非数字值、 null值和缺失字段。

  • 如果窗口包含一个文档,则返回 null。(与 $covarianceSamp 相比,如果窗口包含一个文档,则返回 null。)

  • 如果窗口为空,则返回null 。

  • 如果窗口包含NaN值,则返回NaN 。

  • 如果窗口包含一个或多个全为正数或全为负数的 Infinity 值,则返回 Infinity。返回的 Infinity 值与窗口中的 Infinity 值具有相同的符号。

  • 如果窗口包含具有不同符号的 Infinity 值,则返回 NaN。

  • 如果窗口包含 decimal 值,则返回 decimal 值。

  • 如果前面的点都不适用,则返回double值。

按优先级顺序返回的值如下:

  • NaN

  • Infinity

  • decimal

  • double

创建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 }] )

此示例在 阶段使用$covariancePop $setWindowFields来输出蛋糕销售orderDate 年和quantity 值的总体协方差值:

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 以升序 (1) 对每个分区中的文档进行排序,因此最早的 orderDate 位于最前面。

  • output 在 文档 窗口中使用 运行设置 $covariancePoporderDate年和 值的总体协方差值。quantity

    该窗口包含介于unbounded下限和输出中current文档之间的文档。 这意味着$covariancePop将covariancePopForState字段设置为分区开头和当前文档之间文档的总体协方差值。

在此输出中,总体协方差显示在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 }
给本页内容打分

在此页面上