MongoDB is paged using Skip and limit

  • 2020-05-15 02:27:50
  • OfStack

Data paging can be done using Skip and limit as follows:

Code:


   page1 = db.things.find().limit(20)
   page2 = db.things.find().skip(20).limit(20)
   page3 = db.things.find().skip(40).limit(20) 

Note: can be used for pagination, limit is pageSize, skip is n-1 *pageSize (n-1 = 1,2... Page) skip represents how many pieces of data are skipped to aggregate pipeline optimization
1.$sort + $skip + $limit sequence optimization

If, when performing pipeline aggregation, $sort, $skip, and $limit appear in turn, 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 be executed before $skip.

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

There are two benefits to doing this:

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

2. After $limit is advanced, $sort is next to $limit so that when $sort is processed it stops when the previous "$limit" document is obtained.

When the amount of data is small, this is fine. However, when the amount of data is large, the skip operation will become slow and should be avoided.

(not just mongoDb, but most databases.) You can achieve pagination by changing the rules for querying documents to avoid using skip to skip large amounts of data.

(calculate where the next query should start.)


Related articles: