[SERVER-31601] Ranking(using project) + sort + skip&limit(pagination) not working as expected Created: 15/Oct/17 Updated: 27/Oct/23 Resolved: 18/Oct/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Critical - P2 |
| Reporter: | Tanmay Awasekar | Assignee: | Mark Agarunov |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | Bug, MAREF | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Participants: |
| Description |
|
When I perform a sort using a project fields value like this:
Here I perform a sort using the size of the array. In this example the size of the array is zero, but it can vary if the array is made out of value of a field in the collection.
When the value of skip goes from 4..8..12, many documents are repeated. |
| Comments |
| Comment by Mark Agarunov [ 18/Oct/17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello tanmayawasekar, Thank you for the information. Looking over this, I believe this may have to do with how sort works. When sorting by a field, the only guarantee is that the results will be in order in relation to that field. If you have multiple documents with the same value in the field you are sorting by, there is no guarantee that these documents will be sorted in the same order every time. That is, if you have the following documents, sorted by x:1:
The only guarantee $sort is making is that the documents with x:1 will come before x:2. If this query was run again, it could produce the following, also valid and properly sorted, result set:
If you are using limit and skip, the order of the documents can change between queries. As an example, if the two lists of documents above are the results of running the same query twice, and if you were to use skip: 1 and limit: 1 on the first query, and then skip:3 and limit:1 on the second, they would both return the same document, x:1,y:1, causing the duplicate results you are seeing. To ensure that the sort order is consistent, you could add a unique field to sort on, such as _id as mentioned in Thanks, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Tanmay Awasekar [ 18/Oct/17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
MongoDB version - v3.4.9,
For the dataset, you can replicate the issue using any collection which has more than 20 documents. Just copy the query I have given above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mark Agarunov [ 17/Oct/17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello tanmayawasekar, Thank you for the report. To get a better idea of what may be causing this behavior, I'd like to get some additional information.
I've created a secure upload portal so that you can send us these files privately. Thanks, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Tanmay Awasekar [ 15/Oct/17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Here is the entire example again.
|