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