Database

[{"_id":"1","score":200,"invalidated":false,"assigned_agent":null},{"_id":"2","score":10000,"invalidated":false,"assigned_agent":null},{"_id":"3","score":500,"invalidated":false,"assigned_agent":null}]

Query

db.collection.aggregate([{$match:{invalidated:false,assigned_agent:null}},{$setWindowFields:{sortBy:{score:-1,_id:-1},output:{order:{$sum:1,window:{documents:["unbounded","current"]}}}}}])

Result