“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$integral(聚合)

在此页面上

  • 定义
  • 行为
  • 例子

版本 5.0 中的新增功能

$integral

返回曲线下面积的近似值,该近似值使用梯形规则计算得出,其中每组相邻文档使用以下公式形成一个梯形:

  • 积分区间的$setWindowFields 阶段中的 sortBy 字段值。

  • 输入字段表达式的结果值为 $integral 中的 Y 轴值。

$integral 只能在 $setWindowFields 阶段使用。

$integral 事务语法:

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

$integral 接受包含以下字段的文档:

字段
说明
输入

指定要计算的表达式。 您必须提供一个返回数字的表达式。

一个指定时间单位的 string。请使用以下任意一个字符串:

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

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

如果省略window ,则使用具有无边界上限和下限的默认窗口。

创建一个powerConsumptioncollection,其中包含电表设备每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对collection中的文档进行分区

  • sortBy: { timeStamp: 1 }timeStamp 以升序 (1) 对每个分区中的文档进行排序,因此最早的 timeStamp 位于最前面。

  • output kilowatts使用在powerMeterKilowattHours 范围 $integral窗口中运行的 在名为 的新字段中设置 整数值。

    • 输入表达式设置为 "$kilowatts",用于积分计算中的 Y 轴值。

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

    • 窗口包含介于 unbounded 下限与输出中 current 文档之间的文档。这意味着 $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(聚合)

在此页面上