在两到两个文档之间获得时间差
我正在做一个小型的物联网项目,我想展示一些我从设备中获得的有用信息。
设备是一个控制一个灯泡的开关
{
"_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")
}
按钮
- 该 下,
- 在最长的灯光
- 的灯光持续时间处
- 超过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:
- The mean duration of when the light was on
- The longest duration of the lights on
- Longest duration of lights off
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个:
mongo playground
for Mongodb版本< 5.0您需要其他解决方案。作为起点是这个:
Try this one:
Mongo Playground
For MongoDB version < 5.0 you need a different solution. As starting point is this one: