This is very interesting!
I have recently implemented a rank function less elegantly:
- Count number of users with lower score
- Query users with same score, fetch, and loop through array until the desired user is found, remember index.
- Add the count of1 to the index of 2 and add 1.
However, I wonder whether your aggregation approach goes through all the documents in the collection or stops when it reaches the user — given an index on the PLR field.
I also wonder whether it is useful to instead regularly go through all documents and update a rank attribute.