Database
[{"_id":ObjectId("178768747638364736373637"),"start_time":ISODate("2019-02-03T12:00:00.000Z"),"finish_time":ISODate("2019-02-03T12:01:00.000Z"),"a":1},{"_id":ObjectId("266747364736363536353555"),"start_time":ISODate("2019-02-03T12:00:00.000Z"),"finish_time":ISODate("2019-02-03T12:03:00.000Z"),"a":2},{"_id":ObjectId("367463536453623546353625"),"start_time":ISODate("2019-02-03T12:00:00.000Z"),"finish_time":ISODate("2019-02-03T12:08:00.000Z"),"a":1}]
Query
db.collection.aggregate([{$facet:{totalCount:[{$count:"value"}],pipelineResults:[{"$project":{duration:{"$divide":[{"$subtract":["$finish_time","$start_time"]},1000]/** duration is in seconds*/},Percentile95Index:{"$multiply":[0.95,3]}}}]}},{$unwind:"$totalCount"},{$unwind:"$pipelineResults"},{$replaceRoot:{newRoot:{$mergeObjects:["$pipelineResults",{totalCount:"$totalCount.value"}]}}}])