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:{$filter:{input:[{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"}],cond:{$and:[{$eq:["$device_id",{$toString:"$$this.device_id"}]},{$lte:["$start_time","$$this.timestamp"]},{$gte:["$end_time","$$this.timestamp"]}]}}}}},{/** default for preserveNullAndEmptyArrays is false anyway*/$unwind:"$timestamps"},{$replaceWith:"$timestamps"}])

Result