Database

[{_id:ObjectId("6591f2fe8424cddac5b1d4aa"),captureTime:1704063737,deviceId:200220,sensorId:1431},{_id:ObjectId("6591f2fe8424cddac5b1d4ab"),captureTime:1704063837,deviceId:200220,sensorId:1431}]

Query

db.collection.aggregate([{$group:{"_id":{"sensorId":"$sensorId",interval:{$dateTrunc:{date:{"$toDate":{"$multiply":["$captureTime",1000]}},unit:"hour"}}},"count":{"$sum":1},"time_f":{"$first":{"$dateToString":{"format":"%Y:%m:%d %H:%M:%S","date":{"$toDate":{"$multiply":["$captureTime",1000]}}}}},"time_l":{"$last":{"$dateToString":{"format":"%Y:%m:%d %H:%M:%S","date":{"$toDate":{"$multiply":["$captureTime",1000]}}}}},"device_Id_f":{"$first":"$deviceId"},"sensor_Id_f":{"$first":"$sensorId"}}},{$set:{_id:{sensorId:"$_id.sensorId",interval:{"$dateToString":{"format":"%Y:%m:%d %H:%M:%S","date":"$_id.interval"}},year:{$year:"$_id.interval"},month:{$month:"$_id.interval"},day:{$dayOfMonth:"$_id.interval"},hour:{$hour:"$_id.interval"}}}},{$set:{time_interval:"$_id.interval"}}])

Result