Database

[{x:1},{x:3},{x:5},{x:20},{x:22}]

Query

db.collection.aggregate([{$setWindowFields:{partitionBy:"",sortBy:{x:1},output:{c:{$push:"$x",window:{range:[-3,0]}}}}},{"$set":{"c":{"$cond":{"if":{"$gt":[{"$size":"$c"},1]},"then":0,"else":1}}}},{$setWindowFields:{partitionBy:"",sortBy:{x:1},output:{g:{$sum:"$c",window:{documents:["unbounded","current"]}}}}},{"$group":{"_id":"$g","count":{$sum:1},"max":{"$max":"$x"},"min":{"$min":"$x"}}}])

Result