Database

[{gateway:"G1",sensor:"S1",timestamp:155158152},{gateway:"G1",sensor:"S2",timestamp:15155158152},{gateway:"G2",sensor:"S1",timestamp:15571158152},{gateway:"G2",sensor:"S1",timestamp:18855158152},{gateway:"G1",sensor:"S1",timestamp:15515815231},{gateway:"G2",sensor:"S1",timestamp:1551585152}]

Query

db.collection.aggregate([{$match:{sensor:"S1"}},{$group:{_id:"$gateway",minTimestamp:{$min:"$timestamp"},maxTimestamp:{$max:"$timestamp"},}},{$project:{_id:0,gateway:"$_id",duration:{$concat:[{$toString:"$minTimestamp"},"-",{$toString:"$maxTimestamp"}]}}}])

Result