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)