I have a collection in a MongoDB and I would like to get the average of the value in 24 hour time intervals for a given day.
Here is my collection:
[
{
"_id": 1,
"value": 50,
"created_at": 1614217700
},
{
"_id": 2,
"value": 60,
"created_at": 1614219300
},
{
"_id": 3,
"value": 100,
"created_at": 1614226200
},
{
"_id": 4,
"value": 80,
"created_at": 1614227400
}
]
The documents 3 & 4 are in the same hour slot, so the average of 80 and 100 is 90. Here is an example of what I expect as a result (or something similar).
[
{
"time": 0,
"avg_value": null
},
{
"time": 1,
"avg_value": 50
},
{
"time": 2,
"avg_value": 60
},
{
"time": 3,
"avg_value": null
},
{
"time": 4,
"avg_value": 90
},
,
,
,
{
"time": 23,
"avg_value": null
}
]
Here you can find my example: https://mongoplayground.net/p/YaAeebapbD-
Any ideas will be greatly appreciated. Thank you!