$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描述
input

Specifies the expression to evaluate. 指定要计算的表达式You must provide an expression that returns a number.必须提供一个返回数字的表达式。

unit

A string that specifies the time unit. 指定时间单位的字符串。Use one of these strings:使用以下字符串之一:

  • "week"
  • "day"
  • "hour"
  • "minute"
  • "second"
  • "millisecond"

If the sortBy field is not a date, you must omit a unit. 如果sortBy字段不是日期,则必须省略一个unitIf you specify a unit, you must specify a date in the sortBy field.如果指定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.使用范围窗口中运行的$integral在名为powerMeterKlowattHours的新字段中设置kilowatts(千瓦)积分值。

    • The input expression is set to "$kilowatts", which is used for the y axis values in the integral calculation.输入表达式设置为"$kilowatts",用于积分计算中的y轴值。
    • The $integral unit is set to "hour" for the timeStamp field, which means $integral returns the kilowatt-hours energy consumption.timeStamp字段的$integral单位设置为"hour",这意味着$integral返回千瓦时能耗。
    • The window contains documents between an unbounded lower limit and the current document in the output. 窗口包含输出中处于unbounded下限和current文档之间的文档。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.这意味着$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聚合》电子书。

←  $indexOfCP (aggregation)$isArray (aggregation) →