On this page本页内容
New in version 5.0.在版本5.0中新增。
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:返回曲线下面积的近似值,该值使用梯形规则计算,其中每组相邻文档使用
$setWindowFields
stage for the integration intervals.$setWindowFields
阶段中的sortBy
字段值。$integral
for the y axis values.input
字段表达式为y轴值生成$integral
值。$integral
is only available in the 仅在$setWindowFields
stage.$setWindowFields
阶段可用。
$integral
syntax:语法:
{ $integral: { input: <expression>, unit: <time unit> } }
$integral
takes a document with these fields:获取包含以下字段的文档:
input |
|
unit |
|
If you omit a window, 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:powerConsumption
集合,其中包含电表设备每隔30秒测量的用电量(千瓦):
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:$setWindowFields
阶段中的$integral
输出每个电表设备测量的电能消耗(以千瓦时为单位):
db.powerConsumption.aggregate( [ { $setWindowFields: { partitionBy: "$powerMeterID", sortBy: { timeStamp: 1 }, output: { powerMeterKilowattHours: { $integral: { input: "$kilowatts", unit: "hour" }, window: { range: [ "unbounded", "current" ], unit: "hour" } } } } } ] )
In the example:在该示例中:
partitionBy: "$powerMeterID"
powerMeterID
.powerMeterID
对集合中的文档进行分区。sortBy: { timeStamp: 1 }
timeStamp
in ascending order (1
), so the earliest timeStamp
is first.timeStamp
以升序(1
)对每个分区中的文档进行排序,因此最早的timeStamp
是第一个。output
sets the 使用范围窗口中运行的kilowatts
integral value in a new field called powerMeterKilowattHours
using $integral
that is run in a range window.$integral
在名为powerMeterKlowattHours
的新字段中设置kilowatts
(千瓦)积分值。
"$kilowatts"
, which is used for the y axis values in the integral calculation."$kilowatts"
,用于积分计算中的y轴值。$integral
unit is set to "hour"
for the timeStamp
field, which means $integral
returns the kilowatt-hours energy consumption.timeStamp
字段的$integral
单位设置为"hour"
,这意味着$integral
返回千瓦时能耗。unbounded
lower limit and the current
document in the output. unbounded
下限和current
文档之间的文档。$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.$integral
返回从分区开始(每个电能表分区中的第一个数据点)到输出中当前文档的时间戳之间文档的总千瓦时能耗。In this example output, the energy consumption measured by meters 1 and 2 are shown in the 在本例输出中,电表1和2测量的能耗显示在powerMeterKilowattHours
field: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 }
For an additional example about IOT Power Consumption, see the Practical MongoDB Aggregations e-book.有关IOT功耗的其他示例,请参阅《实用MongoDB聚合》电子书。