Reputation: 229
I have data like this in my collection:
...
{ name: 'ad1cvbc', price: 300 },
{ name: 'tbcvbc3', price: 500 },
{ name: 'yjc5vbc', price: 500 },
{ name: 'at7vvbc', price: 500 },
{ name: 'a86v5bc', price: 500 },
{ name: 'a23dvbc', price: 500 },
{ name: 'adth4bc', price: 500 },
{ name: 'e34bc', price: 700 },
...
And I have an aggregate like this:
[
{ $match: { ... } },
{ $sort: { price: -1 } },
{ $skip: x },
{ $limit: 20 }
]
Where x
is increased with 20 every single time I wants more data, but the problem is that, because I have multiple price
with same value( and I have more than 20) sometimes I get the same document
. How cand I assure that the order is the same and I don't get duplicates when I have many rows with the field for the sort having the same value.
Edit: So if I want to get 3 items each time I may have the following results:
1st call result:
{ name: 'ad1cvbc', price: 300 },
{ name: 'tbcvbc3', price: 500 },
{ name: 'yjc5vbc', price: 500 } // <- this
2st call result:
{ name: 'a23dvbc', price: 500 },
{ name: 'yjc5vbc', price: 500 }, // <- this
{ name: 'at7vvbc', price: 500 }
1 document is repeated because MongoDB doesn't get each time the items in order when the documents has the same value for the sort field.
Upvotes: 1
Views: 270
Reputation: 75914
Something like
[
{ $match: { ... } },
{ $sort: { name:-1, price: -1 } },
{ $skip: x },
{ $limit: x + 20 }
]
Upvotes: 1