MongoDB use Skip and limit pagination _mongodb

Source: Internet
Author: User
Tags mongodb

Use skip and limit to do data paging as follows:

Code:

 Page1 = Db.things.find (). Limit ()
 Page2 = Db.things.find (). Skip (limit) page3
 = Db.things.find (). Skip (limit). (20) 

Note: Can be used for paging, limit is Pagesize,skip is n-1 page *pagesize (n-1 denotes 1,2 ... page) skip indicates how many pieces of data are skipped, optimization of the aggregation pipeline
1. $sort + $skip + $limit order optimization

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

{$sort: {Age:-1}},
{$skip: ten},
{$limit: 5}

Then the actual order of execution is:

{$sort: {Age:-1}},
{$limit:},
{$skip: 10}

$limit will go ahead to $skip.

At this point $limit = before optimizing $skip+ optimization before $limit

There are two advantages to doing this:

1. After the $limit pipeline, the number of documents in the pipeline will be reduced "in advance", which will save memory and improve memory utilization efficiency.

2. After $limit, $sort close to $limit so that when the $sort is carried out, it will stop when the previous "$limit" document is available.

When the amount of data is very small, it is completely fine to do pagination. However, when the volume of data is very large, skip operation will be very slow, should avoid the use.

(Not just MongoDB, most databases are.) You can change the rules of the query document to achieve a paging effect and avoid skipping a large amount of data by using skip.

(by calculation, where the next query should start)

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.