【问题标题】:How to use Aggregation Framework in MongoDB to find out hourly, daily, weekly, yearly temperature data?如何在 MongoDB 中使用聚合框架来查找每小时、每天、每周、每年的温度数据?
【发布时间】:2021-11-05 13:13:48
【问题描述】:

示例数据包(每 5 秒存储一次):

{
  _id : "74uy4bf71880088hre",
   date : 2021-05-07T03:54:27.318+00:00,
   deviceId : "12345t6",
   temperature:21,
}

我在 MongoDB 的这个数据包格式中有最近几个月的数据,我想创建一个聚合管道来检索每小时、每天(11 月 3 日至 10 月 3 日)、每周(过去 7 周)、每月的数据。

请任何人知道如何编写它。提前致谢。

额外信息可以询问。

除了聚合框架有任何其他建议。

每小时的预期产出:

[
  {
        "dataType": "Temperature",
        "deviceID": "12345t6",
        "timing": "Hourly",
        "temperature": 0,
        "date": "2021-11-02T23:30:00.000Z",
        "hours": 1,
        
    },
    {
        "dataType": "Temperature",
        "deviceID": "12345t6",
        "timing": "Hourly",
        "temperature": 0,
        "date": "2021-11-03T00:00:00.000Z",
        "hours": 2,
    },
 .......hours:24
]

这里的“小时”:1 表示下午 0-1 点,而“小时”:2 表示下午 1-2 点,依此类推。

【问题讨论】:

  • 你能分享你的预期输出吗?通常,您的测试用例将是按不同间隔分组的计数,这适合您的用例吗?
  • 温度应该存储为数字,而不是字符串。

标签: node.js mongodb aggregation-framework


【解决方案1】:

数据

[
  {
    date: "2021-05-07T03:54:27.318+00:00",
    deviceId: "12345t6",
    temperature: "21"
  },
  {
    date: "2021-05-07T05:54:27.318+00:00",
    deviceId: "12345t6",
    temperature: "23"
  },
  {
    date: "2021-05-08T05:54:27.318+00:00",
    deviceId: "12345t6",
    temperature: "25"
  }
]

聚合

db.collection.aggregate([
  {
    "$facet": {
      "hourly": [
        {
          "$group": {
            "_id": {
              $dateTrunc: {
                date: {
                  "$toDate": "$date"
                },
                unit: "hour"
              }
            },
            "avg": {
              "$avg": {
                "$toInt": "$temperature"
              }
            }
          }
        }
      ],
      "daily": [
        {
          "$group": {
            "_id": {
              $dateTrunc: {
                date: {
                  "$toDate": "$date"
                },
                unit: "day"
              }
            },
            "avg": {
              "$avg": {
                "$toInt": "$temperature"
              }
            }
          }
        }
      ],
      "weekly": [
        {
          "$group": {
            "_id": {
              $dateTrunc: {
                date: {
                  "$toDate": "$date"
                },
                unit: "week"
              }
            },
            "avg": {
              "$avg": {
                "$toInt": "$temperature"
              }
            }
          }
        }
      ],
      "monthly": [
        {
          "$group": {
            "_id": {
              $dateTrunc: {
                date: {
                  "$toDate": "$date"
                },
                unit: "month"
              }
            },
            "avg": {
              "$avg": {
                "$toInt": "$temperature"
              }
            }
          }
        }
      ]
    }
  }
])

mongoplayground

【讨论】:

  • 对于每周平均值,您应该指定startOfWeek: <Expression>,它默认为星期日,这可能不适合您的应用程序。
猜你喜欢
  • 1970-01-01
  • 2022-06-27
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
相关资源
最近更新 更多