Database
[{x:1},{x:3},{x:5},{x:20},{x:22}]
Query
db.collection.aggregate([{$group:{_id:null,temp:{$push:"$$ROOT"}}},{"$project":{_id:0,"temp_field":{"$reduce":{"input":"$temp","initialValue":{"prev":-99999999,"min":-99999999,"ranges":[]},"in":{"prev":"$$this.x","min":{"$cond":[{$ne:[{"$subtract":["$$this.x","$$value.prev"]},2/**sequence step*/],},"$$this.x","$$value.min"]},"dif":{"$subtract":["$$this.x","$$value.prev"]},"ranges":{"$concatArrays":["$$value.ranges",{"$cond":[{$ne:[{"$subtract":["$$this.x","$$value.prev"]},2/**sequence step*/],},[{max:"$$value.prev",min:"$$value.min"}],[]]}]}}}}}},{"$project":{ranges:{"$concatArrays":["$temp_field.ranges",[{max:"$temp_field.prev",min:"$temp_field.min"}]]}}}])