As a newcomer to MongoDB and Mongoose, I am working with a collection of highscores containing documents structured like this:
{id: 123, user: 'User14', score: 101}
{id: 231, user: 'User10', score: 400}
{id: 412, user: 'User90', score: 244}
{id: 111, user: 'User12', score: 310}
{id: 221, user: 'User88', score: 900}
{id: 521, user: 'User13', score: 103}
+ numerous others...
Currently, I am able to retrieve the top 5 players using the following query:
highscores
.find()
.sort({'score': -1})
.limit(5)
.exec(function(err, users) { ...code... });
While this is useful, I am also interested in querying the position of a specific player, such as user12
, within the highscore list.
Is there a way to achieve this through a query?