Docs HomeMongoDB Manual

$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:返回曲线下面积的近似值,该近似值是使用梯形规则计算的,其中每组相邻文档使用以下公式形成梯形:

$integral is only available in the $setWindowFields stage.仅在$setWindowFields阶段中可用。

$integral syntax:语法:

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

$integral takes a document with these fields:获取包含以下字段的文档:

Field字段Description描述
inputSpecifies the expression to evaluate. 指定要计算的表达式You must provide an expression that returns a number. 必须提供一个返回数字的表达式。
unitA string that specifies the time unit. Use one of these strings: 指定时间单位的string。使用以下字符串之一:
  • "week"
  • "day"
  • "hour"
  • "minute"
  • "second"
  • "millisecond"
If the sortBy field is not a date, you must omit a 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 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.使用在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 $integral unit is set to "hour" for the timeStamp field, which means $integral returns the kilowatt-hours energy consumption.timeStamp字段的积分unit设置为"hour",这意味着$integral返回千瓦时能耗。
    • The window 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.窗口包含在unbounded下限和输出中的current文档之间的文档。这意味着$integral返回文档从分区开始到输出中当前文档的时间戳的总千瓦时能耗,分区开始是每个电表分区中的第一个数据点。

In this example output, the energy consumption measured by meters 1 and 2 are shown in the powerMeterKilowattHours field:在此示例输出中,电表1和2测量的能耗显示在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

See also: 另请参阅:

For an additional example about IOT Power Consumption, see the Practical MongoDB Aggregations e-book.有关IOT功耗的其他示例,请参阅实用MongoDB聚合电子书