Reputation: 1144
Here is simplified version of my schema:
var MySchema = new Schema({
createdDate: {
type: Date,
required: true,
default: Date.now,
index: true
},
vote: {
type: Number,
index: true,
default: 0
}
});
I have large amount of data, so for paging with good performance I use range query like: .find({_id: {$gt: lastId}}).limit(20)
. Now I also want to sort my data by vote field. How should I do this?
Upvotes: 2
Views: 1549
Reputation: 151112
Fairly much the same thing as the looking for a greater value concept, but this time on the "vote", but with another twist:
var query = Model.find({
"vote": { "$lte": lastVoteValue },
"_id": { "$nin": seenIds }
}).sort({ "vote": -1 }).limit(20);
So if you think about what is going on here, since you are doing a "descending" sort you want values of vote that are either "less than or equal to" the last value seen from your previous page.
The second part would be the "list" of previously seen _id
values from either just the last page or possibly more. That part depends on how "granular" your "vote" values are in order to maintain that none of the items already paged are seen in the next page.
So the $nin
operator does the exclusion here. You might want to track how that "vote" value varies to help you decide when to reduce the list of "seenIds".
That's the way to do range queries for paging, but if you need to jump to "specific" pages by number don't forget that you would still need to .limit()
and .skip()
. But this will work with single page jumps, or otherwise just incremental paging.
Upvotes: 2