Definição
Novidade na versão 5.0.
Retorna a aproximação da área abaixo de uma curva, que é calculada usando a regra do cachecol, em que cada conjunto de documentos adjacentes forma um cachecol usando:
valores de campo sortBy no estágio
$setWindowFieldspara os intervalos de integração.input field expression result values in
$integralfor the y axis values.
$integral is only available in the $setWindowFields stage.
$integral sintaxe:
{ $integral: { input: <expression>, unit: <time unit> } }
$integral pega um documento com estes campos:
Campo | Descrição |
|---|---|
Especifica a expressão para avaliar. Você deve fornecer uma expressão que retorne um número. | |
Comportamento
Se você omitir um window, será usada uma janela padrão com limites superior e inferior ilimitados.
Exemplo
Crie uma collection powerConsumption que contenha o uso de energia elétrica em kilosegundos medido por dispositivos de medição em intervalos de 30 segundos:
db.powerConsumption.insertMany( [ { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:10:30Z" ), kilowatts: 2.95 }, { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:11:00Z" ), kilowatts: 2.7 }, { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:11:30Z" ), kilowatts: 2.6 }, { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:12:00Z" ), kilowatts: 2.98 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:10:30Z" ), kilowatts: 2.5 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:11:00Z" ), kilowatts: 2.25 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:11:30Z" ), kilowatts: 2.75 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:12:00Z" ), kilowatts: 2.82 } ] )
This example uses $integral in the $setWindowFields stage to output the energy consumption in kilowatt-hours measured by each meter device:
db.powerConsumption.aggregate( [ { $setWindowFields: { partitionBy: "$powerMeterID", sortBy: { timeStamp: 1 }, output: { powerMeterKilowattHours: { $integral: { input: "$kilowatts", unit: "hour" }, window: { range: [ "unbounded", "current" ], unit: "hour" } } } } } ] )
No exemplo:
partitionBy: "$powerMeterID"particiona os documentos na coleção porpowerMeterID.sortBy: { timeStamp: 1 }classifica os documentos em cada partição portimeStampem ordem crescente (1), para que otimeStampmais antigo seja o primeiro.outputsets thekilowattsintegral value in a new field calledpowerMeterKilowattHoursusing$integralthat is run in a range window.The input expression is set to
"$kilowatts", which is used for the y axis values in the integral calculation.The window contains documents between an
unboundedlower limit and thecurrentdocument in the output. This means$integralreturns the total kilowatt-hours energy consumption for the documents from the beginning of the partition, which is the first data point in the partition for each power meter, to the timestamp of the current document in the output.
Neste exemplo de saída, o consumo de energia medido pelos metros 1 e 2 é mostrado no campo powerMeterKilowattHours :
{ "_id" : ObjectId("60cbdc3f833dfeadc8e62863"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:10:30Z"), "kilowatts" : 2.95, "powerMeterKilowattHours" : 0 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62864"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:11:00Z"), "kilowatts" : 2.7, "powerMeterKilowattHours" : 0.023541666666666666 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62865"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:11:30Z"), "kilowatts" : 2.6, "powerMeterKilowattHours" : 0.045625 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62866"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:12:00Z"), "kilowatts" : 2.98, "powerMeterKilowattHours" : 0.068875 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62867"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:10:30Z"), "kilowatts" : 2.5, "powerMeterKilowattHours" : 0 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62868"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:11:00Z"), "kilowatts" : 2.25, "powerMeterKilowattHours" : 0.019791666666666666 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62869"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:11:30Z"), "kilowatts" : 2.75, "powerMeterKilowattHours" : 0.040625 } { "_id" : ObjectId("60cbdc3f833dfeadc8e6286a"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:12:00Z"), "kilowatts" : 2.82, "powerMeterKilowattHours" : 0.06383333333333334 }
Dica
Para obter um exemplo adicional sobre o consumo de energia IOT, consulte o e-book Aggregations práticas do MongoDB.