Menu Docs

$covariancePop (aggregation)

Novidades na versão 5.0.

$covariancePop

Returns the population covariance of two numeric expressions that are evaluated using documents in the $setWindowFields stage window.

$covariancePop está disponível somente no estágio $setWindowFields.

$covariancePop sintaxe:

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

$covariancePop comportamento:

  • Ignora valores não numéricos, valores null e campos ausentes em uma janela.

  • Se a janela contiver um documento, retorna null. Compare com $covarianceSamp, que retorna null se a janela contiver um documento.

  • Se a janela estiver vazia, retorna null.

  • Se a janela contiver um valor NaN , retorna NaN.

  • Se a janela contiver um ou mais valores Infinity que são todos positivos ou todos negativos, retorna Infinity. O valor Infinity retornado tem o mesmo sinal que os valores Infinity na janela.

  • Se a janela contiver valores Infinity com sinais diferentes, retornará NaN.

  • Se a janela contiver um valor decimal , retornará um valor decimal .

  • Se nenhum dos pontos anteriores se aplicar, retorna um valor double .

Os valores retornados em ordem de precedência são os seguintes:

  • NaN

  • Infinity

  • decimal

  • double

Crie uma collection cakeSales que contenha vendas de bolo nos estados da Califórnia (CA) e de Washington (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:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
covariancePopForState: {
$covariancePop: [ { $year: "$orderDate" }, "$quantity" ],
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

No exemplo:

  • partitionBy: "$state" divide dos documentos na coleção por state. Existem divisões para CA e WA.

  • sortBy: { orderDate: 1 } classifica os documentos em cada divisão por orderDate em ordem crescente (1), de modo que a orderDate mais antiga seja a primeira.

  • output sets the population covariance values for the orderDate year and quantity values using $covariancePop run in a documentos window.

    The window contains documents between an unbounded lower limit and the current document in the output. 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.

In this output, the population covariance is shown in the covariancePopForState field:

{ "_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 }