Manuel Di Iorio Manuel Di Iorio - 1 year ago 60
Node.js Question

MongoDB - Problems with paginating records using a score sorting

Let's say I have 3 records with these scores:

{_id: 3, score: 0}, {_id: 20, score: 2.5}, {_id: 1, score: 0}

Right now, I query it in this way

score: {$lt: lastScore}
.sort({score: -1})

lastScore is the score of the latest record found from this query. In this way, in the next iteration, I can get the following page of records, without including the previous records.

But there is a huge problem, let's see what will happen with this query:

First of all, the 2 records sorted by score will be taken:
{_id: 20, score: 2.5}, {_id: 3, score: 0}
. So, lastScore will be equal to 0. If you see above, I use
score: {$lt: lastScore}
, so in the next iteration, the latest record will not be found (0 is not minor than lastScore, which also is 0). This problem happen with all the records that have the same score..

Do you have some proposals on how to implement pagination, using the sorting by score with Mongoose ?

PS: quick solution would be to use the .skip() method but it is not really good with big collections and I need a scalable solution

Answer Source

Endend up using the .skip() method, no other solutions for now.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download