Definition定义
New in version 5.0.在版本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:返回曲线下面积的近似值,该值使用梯形规则计算,其中每组相邻文档使用以下公式形成梯形:
sortBy field values in the$setWindowFieldsstage for the integration intervals.$setWindowFields阶段中用于积分间隔的sortBy字段值。input field expression result values iny轴值的$integralfor the y axis values.$integral中的input字段表达式结果值。
$integral is only available in the $setWindowFields stage.$integral仅在$setWindowFields阶段可用。
$integral syntax:语法:
{
$integral: {
input: <expression>,
unit: <time unit>
}
}
$integral takes a document with these fields:获取包含以下字段的文档:
input |
|
unit |
|
Behavior行为
If you omit a window, a default window with unbounded upper and lower limits is used.如果省略window,则使用具有无上限和下限的默认窗口。
Example示例
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"partitions the documents in the collection by通过powerMeterID.powerMeterID对集合中的文档进行分区。sortBy: { timeStamp: 1 }sorts the documents in each partition by按timeStampin ascending order (1), so the earliesttimeStampis first.timeStamp升序(1)对每个分区中的文档进行排序,因此最早的timeStamp位居前列。outputsets thekilowattsintegral value in a new field calledpowerMeterKilowattHoursusing$integralthat is run in a range window.output在一个名为powerMeterKilowattHours的新字段中设置kilowatts积分值,该字段使用在范围窗口中运行的$integral。The input expression is set to"$kilowatts", which is used for the y axis values in the integral calculation.input表达式设置为"$kilowatts"(千瓦),用于积分计算中的y轴值。The$integralunit is set to"hour"for thetimeStampfield, which means$integralreturns the kilowatt-hours energy consumption.timeStamp字段的$integral单位设置为"hour",这意味着$integral返回千瓦时的能耗。The window contains documents between an该unboundedlower limit and thecurrentdocument in the output.window包含的文档位于unbounded下限和输出中的current文档之间。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.$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 }
Tip
For an additional example about IOT Power Consumption, see the Practical MongoDB Aggregations e-book.有关物联网功耗的更多示例,请参阅实用MongoDB聚合电子书。