Database
db={windows:[{"start_time":"2024-05-12T12:00:00Z","end_time":"2024-05-12T14:00:00Z","device_id":"1"},{"start_time":"2024-05-12T07:00:00Z","end_time":"2024-05-12T09:00:00Z","device_id":"2"},{"start_time":"2024-05-12T01:00:00Z","end_time":"2024-05-12T03:00:00Z","device_id":"3"}]}
Query
db.windows.aggregate([{$set:{timestamps:[{device_id:1,timestamp:"2024-05-12T13:00:00Z"},{device_id:3,timestamp:"2024-05-12T13:00:00Z"},{device_id:4,timestamp:"2024-05-12T13:00:00Z"}]}},{$unwind:"$timestamps"},{$match:{$expr:{$and:[{$eq:["$device_id",{$toString:"$timestamps.device_id"}]},{$lte:["$start_time","$timestamps.timestamp"]},{$gte:["$end_time","$timestamps.timestamp"]}]}}},{$replaceWith:"$timestamps"}])