MongoDB's Skip,limit,sort execution order

Source: Internet
Author: User
Tags mongodb

All data are not the same.

First look at skip and limit, when the two are used together, regardless of their position order, default first skip, then limit.

The following figure:


Look at the sort, "6" statement, my data is sorted. After that, the three data are the same, regardless of how the transformation is sorted. (all possibilities are not listed here.) The skip and limit position changes are followed by the sort combination, but the results are still the same)

It can be concluded that when Sort,skip,limit is used together, regardless of the position change, always first sort and then skip, the last limit.


But I want to skip first, and then limit the number of bars to the last sort.

Can be used aggregate, aggregate has the nature of the pipeline flow, $skip, $limit, $sort the execution sequence is consistent with the position.



The position determines the order of execution.




Optimization of the aggregation pipeline:

Http://docs.mongoing.com/manual-zh/core/aggregation-pipeline-optimization.html#agg-sort-skip-limit-sequence


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.