$integral (aggregation)
On this page本页内容
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.$setWindowFields阶段中可用。
$integral syntax:语法:
{
$integral: {
input: <expression>,
unit: <time unit>
}
}
$integral takes a document with these fields:获取包含以下字段的文档:
input | |
unit | string that specifies the time unit. Use one of these strings: string。使用以下字符串之一:
unit. If you specify a unit, you must specify a date in the sortBy field. sortBy字段不是日期,则必须省略一个unit。如果指定unit,则必须在sortBy字段中指定日期。 |
Behavior行为
If you omit a window, a default window with unbounded upper and lower limits is used.如果省略窗口,则使用具有无限制上限和下限的默认窗口。
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 the使用在kilowattsintegral value in a new field calledpowerMeterKilowattHoursusing$integralthat is run in a range window.range窗口中运行的$integral在名为powerMeterKilowattHours的新字段中设置kilowatts(千瓦)积分值。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字段的积分unit设置为"hour",这意味着$integral返回千瓦时能耗。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.unbounded下限和输出中的current文档之间的文档。这意味着$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 }
See also: 另请参阅:
For an additional example about IOT Power Consumption, see the Practical MongoDB Aggregations有关IOT功耗的其他示例,请参阅实用MongoDB聚合电子书。 e-book.