文档菜单
文档首页
/
MongoDB 手册
/ / /

$integral (聚合)

本页内容

  • 定义
  • 行为
  • 示例

版本5.0.

$integral

返回曲线下面积的近似值,使用梯形法则计算,其中每一组相邻文档形成一个梯形,使用在 $setWindowFields 阶段中的 sortBy 字段值进行积分区间。

  • sortBy 字段 $setWindowFields 阶段中的值用于计算。

  • input 字段的 expression 结果值在 $integral 中用于 y 轴值。

$integral 仅在 $setWindowFields 阶段中可用。

$integral 语法

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

$integral 接收一个包含以下字段的文档

字段
描述

指定要评估的 expression。您必须提供一个返回数字的表达式。

指定时间单位的字符串。使用以下字符串之一

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

如果 sortBy 字段不是日期,则必须省略 unit。如果指定 unit,则必须在 sortBy 字段中指定日期。

如果您省略了窗口,则使用默认窗口,其上下限均未定义。

创建一个包含电力使用量(以千瓦计)的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 }
] )

此示例在$setWindowFields阶段使用$integral,以输出每个测量仪表测量的千瓦时能源消耗。

db.powerConsumption.aggregate( [
{
$setWindowFields: {
partitionBy: "$powerMeterID",
sortBy: { timeStamp: 1 },
output: {
powerMeterKilowattHours: {
$integral: {
input: "$kilowatts",
unit: "hour"
},
window: {
range: [ "unbounded", "current" ],
unit: "hour"
}
}
}
}
}
] )

在示例中

  • partitionBy: "$powerMeterID" 分区集合中的文档,分区依据为powerMeterID

  • sortBy: { timeStamp: 1 } 排序每个分区中的文档,按照timeStamp的升序(1),因此最早的timeStamp排在首位。

  • output设置一个新的字段powerMeterKilowattHours中的kilowatts积分值,使用在range窗口中运行的$integral

    • 输入表达式设置为"$kilowatts",这是积分计算中y轴值的用途。

    • 对于 $integral 单位,将其设置为 "hour" 用于 timeStamp 字段,这意味着 $integral 返回的值是千瓦时能耗。

    • 窗口包含从 无界 下限到输出中当前文档之间的文档。这意味着 $integral 返回从 分区 的开始到输出中当前文档的时间戳为止的文档总能耗,这个分区是每个电表的第一个数据点。

在这个示例输出中,电表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 }

提示

另请参阅

有关更多示例,请参阅IOT 功耗,请参阅 实用MongoDB聚合操作 电子书。

返回

$indexOfCP

本页内容