在两到两个文档之间获得时间差

发布于 2025-02-13 01:11:12 字数 716 浏览 0 评论 0原文

我正在做一个小型的物联网项目,我想展示一些我从设备中获得的有用信息。

设备是一个控制一个灯泡的开关

{
    "_id" : ObjectId("62baf5b98218da3bc80e9d61"),
    "light_on" : false,
    "device_id" : "ab082e4d-e617-4c74-8d41-fa316246afac",
    "timestamp" : ISODate("2022-07-01T10:00:24.000+0000")
}
{
    "_id" : ObjectId("62baf5b98218da3bc90e9d6a"),
    "light_on" : true,
    "device_id" : "ab082e4d-e617-4c74-8d41-fa316246afac",
    "timestamp" : ISODate("2022-07-01T10:32:00.000+0000")
}

按钮

  1. 该 下,
  2. 在最长的灯光
  3. 的灯光持续时间处
  4. 超过4个小时的闪电时,灯光在最长

所有持续时间超过了这种请求 不是即使确定它是可行的,因此欢迎任何帮助。

I'm doing a small IoT project, and I want to show some useful informations that I get from the device.

The device is a switch button that controls one light bulb, each time the switch is activated, an event is sent to the database, here is two examples of the documents received:

{
    "_id" : ObjectId("62baf5b98218da3bc80e9d61"),
    "light_on" : false,
    "device_id" : "ab082e4d-e617-4c74-8d41-fa316246afac",
    "timestamp" : ISODate("2022-07-01T10:00:24.000+0000")
}
{
    "_id" : ObjectId("62baf5b98218da3bc90e9d6a"),
    "light_on" : true,
    "device_id" : "ab082e4d-e617-4c74-8d41-fa316246afac",
    "timestamp" : ISODate("2022-07-01T10:32:00.000+0000")
}

From this data, I want to extract:

  1. The mean duration of when the light was on
  2. The longest duration of the lights on
  3. Longest duration of lights off
  4. All duration that exceeded 4 hours of lightning on

This kind of request needs me to compare the documents two by two, which I never did in Mongo, and I'm not even sure it's doable, so any help is welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2025-02-20 01:11:13

尝试这个:

db.collection.aggregate([
  {
    $setWindowFields: {
      partitionBy: "$device_id",
      sortBy: { timestamp: 1 },
      output: {
        following: {
          $shift: {
            output: "$timestamp",
            by: 1
          }
        }
      }
    }
  },
  {
    $set: {
      duration: {
        $dateDiff: {
          startDate: "$timestamp",
          endDate: "$following",
          unit: "second"
        }
      }
    }
  },
  {
    $facet: {
      mean: [
        {
          $group: {
            _id: {
              device_id: "$device_id",
              light_on: "$light_on"
            },
            mean: { $avg: "$duration" },
            longest: { $max: "$duration" }
          }
        }
      ],
      exceeded: [
        {
          $match: {
            light_on: true,
            duration: { $gt: 14400 } // 4 hours = 14400 seconds
          }
        }
      ]
    }
  }
])

mongo playground

for Mongodb版本< 5.0您需要其他解决方案。作为起点是这个:

db.collection.aggregate([
  { $sort: { timestamp: -1 } },
  {
    $group: {
      _id: "$device_id",
      timestamp: { $push: "$ROOT" }
    }
  },
  {
    $set: {
      timestamp: {
        $reduce: {
          input: "$timestamp",
          initialValue: [],
          in: {
            $concatArrays: [
              "$value",
              [
                {
                  $mergeObjects: [
                    "$this",
                    { following: { $last: "$value.timestamp" },
                      duration: {
                        $dateDiff: {
                          endDate: { $last: "$value.timestamp" },
                          startDate: "$this.timestamp",
                          unit: "second"
                        }
                      }
                    }
                  ]
                }
              ]
            ]
          }
        }
      }
    }
  },
  ... some cosmetic, see above
])

Try this one:

db.collection.aggregate([
  {
    $setWindowFields: {
      partitionBy: "$device_id",
      sortBy: { timestamp: 1 },
      output: {
        following: {
          $shift: {
            output: "$timestamp",
            by: 1
          }
        }
      }
    }
  },
  {
    $set: {
      duration: {
        $dateDiff: {
          startDate: "$timestamp",
          endDate: "$following",
          unit: "second"
        }
      }
    }
  },
  {
    $facet: {
      mean: [
        {
          $group: {
            _id: {
              device_id: "$device_id",
              light_on: "$light_on"
            },
            mean: { $avg: "$duration" },
            longest: { $max: "$duration" }
          }
        }
      ],
      exceeded: [
        {
          $match: {
            light_on: true,
            duration: { $gt: 14400 } // 4 hours = 14400 seconds
          }
        }
      ]
    }
  }
])

Mongo Playground

For MongoDB version < 5.0 you need a different solution. As starting point is this one:

db.collection.aggregate([
  { $sort: { timestamp: -1 } },
  {
    $group: {
      _id: "$device_id",
      timestamp: { $push: "$ROOT" }
    }
  },
  {
    $set: {
      timestamp: {
        $reduce: {
          input: "$timestamp",
          initialValue: [],
          in: {
            $concatArrays: [
              "$value",
              [
                {
                  $mergeObjects: [
                    "$this",
                    { following: { $last: "$value.timestamp" },
                      duration: {
                        $dateDiff: {
                          endDate: { $last: "$value.timestamp" },
                          startDate: "$this.timestamp",
                          unit: "second"
                        }
                      }
                    }
                  ]
                }
              ]
            ]
          }
        }
      }
    }
  },
  ... some cosmetic, see above
])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文