MongoDB / Mongoose find results up to you

Say I have a mongoose model called glasses. I order it with the column name "points". And having recognized my document, passing in the userid of the document column name is userid.

How can I find out certain information, such as "Are people xx better than you?"

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

How many searches do you need to β€œscroll” until a match is found for your document?

+6
source share
2 answers

A query for user points, and then a query to count users with points above.

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

For scalable performance, you need to index userId and points separately. In your circuit:

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

It should be faster than any solution to reduce the map.

+5
source

This issue has already been resolved in another answer fooobar.com/questions/1010524 / ...

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

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

+2
source

All Articles