Database
[{"createdAt":1688383980100,"win":true},{"createdAt":1688383980200,"win":false},{"createdAt":1688383980300,"win":true},{"createdAt":1688383980400,"win":true},{"createdAt":1688383980500,"win":false}]
Query
db.collection.aggregate([{$setWindowFields:{sortBy:{createdAt:1},output:{isStreakEnd:{$push:"$win",window:{documents:[0,1]}}}}},{$match:{win:true}},{$project:{win:1,isStreakEnd:{$cond:[{$eq:[{$size:"$isStreakEnd"},2]},{$not:{$last:"$isStreakEnd"}},true]}}},{$setWindowFields:{sortBy:{createdAt:1},output:{index:{$sum:1,window:{documents:["unbounded","current"]}}}}},{$match:{isStreakEnd:true}},{$setWindowFields:{sortBy:{createdAt:1},output:{streakSize:{$push:"$index",window:{documents:[-1,0]}}}}},{$project:{streakSize:{$cond:[{$eq:[{$size:"$streakSize"},2]},{$subtract:[{$last:"$streakSize"},{$first:"$streakSize"}]},{$first:"$streakSize"}]}}},{$sort:{streakSize:-1}},{$limit:1}])