[SERVER-62952] Order of $sort and $limit aggregation Created: 25/Jan/22 Updated: 02/Jun/22 Resolved: 31/Jan/22 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Aniket Jha | Assignee: | Edwin Zhou |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL |
| Participants: |
| Description |
|
Does the order of $skip and $limit matter in pipeline? I use mongoose aggregation which run a pipeline of operations. With skip and limt appended at end.
Our pipeline simply looks like
What I observed during pagination that $limit is being respected but $skip is happening only at end of pipeline. For example: page1: skip = 0, limit = 10 page 2: skip = 10, limit = 10 This was causing an issue in our pipeline and it was getting slow for pagination. QUESTION: Is it expected behaviour? And we were using it in an unintented way? SOLUTION: What we ended up doing was to move $limit followed by $skip. page 1: skip = 0, limit = 10 page 2: skip = 10, limit = 10 our pipeline now looks like:
Here is collection scehema for your refernce:
QUESTION:
|
| Comments |
| Comment by Edwin Zhou [ 31/Jan/22 ] |
|
Thanks for your report. This behavior appears expected given the order of operations displayed in your query. Using skip and limit last would mean that the $lookup stages will need to first lookup all of the results of the $match stage. You've mentioned this key point:
For issues and questions like this in the future, we'd like to encourage you to start by asking our community for help by posting on the MongoDB Developer Community Forums. If the discussion there leads you to suspect a bug in the MongoDB server, then we'd want to investigate it as a possible bug here in the SERVER project. Best, |
| Comment by Aniket Jha [ 28/Jan/22 ] |
|
Hey team If possible can you verify the issue and pick to see if it is a bug or it is expected behavior as soon as possible. I'm watching this thread and can provide any more details if required. 😊 |