MongoDB / Mongoose find number of results before you

Let's say I have a mongoose model with the name "points". I'm ordering it with the column name "points". And finding out my document by passing in my userid to the document column name userid.

How can I be able to find out certain information such as "there are xx persons better than you?"

In this case how many documents that have higher points than you?

How many searches does it need to "loop" through until the match of your document is there?


Query for the user's points and then query for the count of users with points higher than that.

Points.findOne({userId: userId}, (err, doc) => {
    Points.count({points: {$gt: doc.points}}, (err, count) => {
        // do something with count...

For scalable performance, you'd want to separately index userId and points. In your schema:

userId: {type: ObjectId, index: true},
points: {type: Number, index: true},

This should be faster than any map-reduce solution.

This problem is already solved in another answer

You can use mapReduce for small dataset or better, maintain a separate ordered collection.

If you want to use mongoose, the Model object has Model.mapReduce method too with the same syntax.

Need Your Help hosting - forms authentication not working for .htm files hosting forms-authentication

I've put a password around a website for preview purposes - it has .htm files generated by a content management system and .aspx files.

Jar not found when executing class

java command-line classpath

I'm working through a ANTLR (a language processing library) book and there are many examples that should be easy to compile using the command line.