MongoDB聚合运算符:$integral

文章目录

$integral聚合运算符只能用在$setWindowField阶段,返回曲线下面积的近似值,该曲线是使用梯形规则计算的,其中每组相邻文档使用以下公式形成一个梯形:

  • $setWindowFields阶段中用于积分间隔的sortBy字段值
  • $integralinput字段表达式用于Y轴的值

语法

js 复制代码
{
   $integral: {
      input: <expression>,
      unit: <time unit>
   }
}

$integral的参数说明

  • input,数值类型的表达式
  • unit,指定时间单位的字符串,可以是:"week""day""hour""minute""second""millisecond"。如果sortBy字段不是日期类型,必须省略单位,如果指定了单位,就必须确保sortBy字段为日期类型

使用

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

举例

使用下面的脚本创建powerConsumption集合,包含仪表设备每隔 30 秒测量一次的用电量(以千瓦为单位):

js 复制代码
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输出每个仪表装置测量的能耗(千瓦时):

js 复制代码
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从小到大进行排序,最早的timeStamp排在最前面。
  • output使用$integral将一个区域窗口的kilowatts积分值放到新字段powerMeterKilowattHours
    • input表达式为$kilowatts,用于积分计算的Y轴值
    • $integral unittimeStamp的积分单位设置为hour小时,$integral返回千瓦时的能源消耗
    • 窗口在输出中包含了无下限到当前文档。这意味着$integral返回从分区开头(每个电表的分区中的第一个数据点)到输出中当前文档的时间戳的文档的总千瓦时能耗。

在本例输出中,电表1和2测量的能耗显示在powerMeterKilowattHours字段中:

json 复制代码
{ "_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 }
相关推荐
liliangcsdn1 小时前
如何使用python创建和维护sqlite3数据库
数据库·sqlite
TDengine (老段)7 小时前
TDengine 数学函数 DEGRESS 用户手册
大数据·数据库·sql·物联网·时序数据库·iot·tdengine
TDengine (老段)7 小时前
TDengine 数学函数 GREATEST 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
安当加密8 小时前
云原生时代的数据库字段加密:在微服务与 Kubernetes 中实现合规与敏捷的统一
数据库·微服务·云原生
爱喝白开水a8 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
想ai抽8 小时前
深入starrocks-多列联合统计一致性探查与策略(YY一下)
java·数据库·数据仓库
武子康8 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql
longgyy8 小时前
5 分钟用火山引擎 DeepSeek 调用大模型生成小红书文案
java·数据库·火山引擎
ytttr8739 小时前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#
盒马coding10 小时前
第18节-索引-Partial-Indexes
数据库·postgresql