Database
[{_id:1,"name":"A","startTime":"1634626355","endTime":"1634631405"},{_id:2,"name":"A","startTime":"1634631406","endTime":"1634631864"},{_id:3,"name":"A","startTime":"1634631865","endTime":"1634656048"},{_id:4,"name":"A","startTime":"1634712642","endTime":"1634718856"}]
Query
db.collection.aggregate([{$addFields:{endDateTime:{"$toDate":{"$multiply":[{$toLong:"$endTime"},1000]}}},},{"$lookup":{"from":"collection",let:{end:"$endDateTime"},pipeline:[{"$addFields":{startDateTime:{"$toDate":{"$multiply":[{$toLong:"$startTime"},1000]}}}},{$match:{$expr:{$and:[{$lte:[{$subtract:["$startDateTime","$$end"]},300000]},{$lte:["$$end","$startDateTime"]}]}}}],"as":"lessThan5min"}},{"$unwind":"$lessThan5min"},{"$replaceRoot":{"newRoot":"$lessThan5min"}}])