Reputation: 1466
I'm having trouble to get my pagination with Mongoose and NodeJs working. In my collection I'm using the ids to create a pagination like so:
var limit = req.params.limit ? req.params.limit : 1;
limit ++;
if(req.params.start){
var start = mongoose.Types.ObjectId(req.params.start);
Canvas.find().where({_id : {$lte : start}}).sort({"_id": -1}).limit(limit).exec(function(err, items){
if(items.length > limit -1){
var last = items.pop();
items.push({next : last._id});
}
});
}else{
Canvas.find().sort({"_id": -1}).limit(limit).exec(function(err, items){
if(items.length > limit -1){
var last = items.pop();
items.push({next : last._id});
}
});
}
And this approach works really fine in this context. I don't want to use skip() because of performance. I preferably like to use a similar implementation when I create mhy toplist of canvases.
Each canvas have votes, a number, that I like to sort by descending. But the problem is that then I no longer can use the ids $lte because that's not the order they come back in since ObjectId is kind of like a timestamp also.
Is there a way of using the next variable, as shown above, as a start in the next page selection or any other implementation without using skip or at least using it with ok performance?
Upvotes: 2
Views: 4954
Reputation: 1935
Umm, as a little tweak to your code you could use sort({ votes: -1, _id: -1})
and paginate using votes
as the primary and _id
as the secondary criterion.
Apart from that, if you're at a stage when you have so many records that skip()
is really hurting, you can maintain a separate cache by bucketing ranges of votes. This will incur some additional maintenance in updating the cache on boundary crosses and so on, but will work pretty smooth once set up properly. It's really upto you to decide how you want to go forward. Might as well try both and see what works best for you.
Upvotes: 2