I have IoT sensors which keep sending events every second, and storing the events in MongoDB below format:
{
"_id" : "stp_23",
"HP" : [
1261.0,
1357.0,
1337.0,
1250.0,
1269.0,
1134.0,
1219.0,
1269.0,
1166.0,
1361.0,
1246.0,
... ]
"TS" : [
"2019-12-20T00:00:04.6440124Z",
"2019-12-20T00:00:14.6440124Z",
"2019-12-20T00:00:24.6440124Z",
"2019-12-20T00:00:34.6450042Z",
"2019-12-20T00:00:44.6450042Z",
"2019-12-20T00:00:54.6450042Z",
"2019-12-20T00:01:55.6460113Z",
"2019-12-20T00:02:05.6460113Z",
"2019-12-20T00:02:15.6460113Z",
"2019-12-20T00:02:35.6460113Z"
... ]
"_id" : "stp_24" : ....
}
I need to create a aggregation every 5 minutes based on TS values. I have followed the below query to execute, since TS is array of timestamp getting Error
Unsupported conversion from array to date in $convert with no onError value
db.hp_daily.aggregate([
{ "$group": {
"_id": {
"$toDate": {
"$subtract": [
{ "$toLong": { "$toDate": "$TS" } },
{ "$mod": [ { "$toLong": { "$toDate": "$TS" } }, 1000 * 60 * 5 ] }
]
}
},
"count": { "$sum": 1 }
}}
])
I'm looking for a result like this:
[
{
"_id" : "stp_23",
"ts" : ISODate("2019-12-20T00:05:00.000Z"),
"avgHP" : 1200
},
{
"_id" : "stp_24",
"ts" : ISODate("2019-12-20T00:10:00.000Z"),
"avgHP" : 1350
}
]