0

My User collection contains the following documents:

{"_id" : 1, point_count: "12"},
{"_id" : 2, point_count: "19"},
{"_id" : 3, point_count: "13"},
{"_id" : 4, point_count: "1233"},
{"_id" : 5, point_count: "1"},
... and about 1000 more

My question is: Is it possible to show ranking of each user based on point_count field when I search by id? let say I use this query to find user with id = 4

db.User.find({_id: 4})

let assume highest point_count in my entire collection is 1233, I'm hoping to get this result

{"_id" : 4, point_count: "1233", rank: 1}

rank 1 because 1233 is the highest point_count

or when I search user with id = 5, my expect result should be

{"_id" : 5, point_count: "1", rank: 1000..something}

1000..something because it is the lowest rank and there are around 1000+ id in the document. Thank you all so much for helping me here!

KevinVuD
  • 581
  • 2
  • 5
  • 25
  • Possible duplicate of [How to get item ranking in list sorted by multiple fields in Mongoose](https://stackoverflow.com/questions/29557791/how-to-get-item-ranking-in-list-sorted-by-multiple-fields-in-mongoose) – Saurabh Mistry Jun 25 '18 at 05:38
  • Possible duplicate of [Add some kind of row number to a mongodb aggregate command / pipeline](https://stackoverflow.com/questions/35174554/add-some-kind-of-row-number-to-a-mongodb-aggregate-command-pipeline) – Dark Knight Jun 25 '18 at 06:42

0 Answers0