Go to MongoDB using skip and limit paging

Source: Internet
Author: User

Some basic statements about MongoDB data paging and sorting Limit,skip users, introducing MongoDB data paging and sorting instance methods.

Use skip and limit to do data paging as follows:

Code:

Page1 = Db.things.find (). Limit (20)

Page2 = Db.things.find (). Skip (+). Limit (20)

Page3 = Db.things.find (). Skip. Limit (20)

Note: Can be used for pagination, limit is Pagesize,skip is page n-1 *pagesize (n-1 represents a few ... page) skip indicates how many data are skipped, and the optimization of the aggregation pipeline.

1. $sort + $skip + $limit order optimization

If the $sort, $skip, $limit occur sequentially when the pipeline aggregation is performed, for example:

{$sort: {Age:-1}},

{$skip: 10},

{$limit: 5}

Then the actual order of execution is:

{$sort: {Age:-1}},

{$limit: 15},

{$skip: 10}

$limit will go ahead to $skip.

At this point $limit = pre-optimization before $skip+ optimization $limit

There are two benefits to this:

1. After the $limit pipeline, the number of documents in the pipeline is reduced "in advance", which saves memory and improves memory utilization efficiency.

2. $limit in advance, $sort close to the $limit, when the $sort is carried out, when the first "$limit" document will be stopped.

When the amount of data is very small, there is no problem doing paging at all. However, when the amount of data is large, the skip operation becomes slow and should be avoided.

(More than MongoDB does, most databases are.) You can achieve paging by changing the rules of the query document, avoiding skipping to skip large amounts of data.

(Calculated to get where the next query should start)

Go to MongoDB using skip and limit paging

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.