Docs 菜单

$integral (aggregation)

在此页面上

版本 5.0 中的新增功能

$integral

Returns the approximation of the area under a curve, which is calculated using the trapezoidal rule where each set of adjacent documents form a trapezoid using the:

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

$integral 语法:

{
$integral: {
input: <expression>,
unit: <time unit>
}
}

$integral接受包含以下字段的文档:

字段
说明

Specifies the 表达式(expression) to evaluate. You must provide an expression that returns a number.

一个指定时间单位的 string。请使用以下任意一个字符串:

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

如果 sortBy 字段不是日期,则必须省略 unit。如果指定unit,则必须在 sortBy 字段中指定日期。

If you omit a 窗口, a default window with unbounded upper and lower limits is used.

Create a powerConsumption collection that contains electrical power usage in kilowatts measured by meter devices at 30 second intervals:

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"
}
}
}
}
}
] )

在示例中:

  • partitionBy: "$powerMeterID"powerMeterID 对集合中的文档分区

  • sortBy: { timeStamp: 1 }timeStamp 升序 (1) 对每个分区中的文档进行排序,因此最早的 timeStamp 位于最前面。

  • output sets the kilowatts integral value in a new field called powerMeterKilowattHours using $integral that is run in a 范围 window.

    • The 输入 expression is set to "$kilowatts", which is used for the y axis values in the integral calculation.

    • The $integral 单位 is set to "hour" for the timeStamp field, which means $integral returns the kilowatt-hours energy consumption.

    • The 窗口 contains documents between an unbounded lower limit and the current document in the output. This means $integral returns 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.

In this example output, the energy consumption measured by meters 1 and 2 are shown in the powerMeterKilowattHours field:

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

提示

另请参阅:

有关物联网功耗的其他示例,请参阅实用 MongoDB 聚合电子书。

在此页面上