Two ways to MongoDB pagination
MongoDB paging query is done by limit (), skip (), sort (), which is a combination of three functions for paging queries.
Here's my test data.
Db.test.find (). Sort ({"Age": 1});
The first of these methods
Query the first page of data: Db.test.find (). Sort ({"Age": 1}). Limit (2);
Query the data for the second page: Db.test.find (). Sort ({"Age": 1}). Skip (2). Limit (2);
Query other pages and so on ...
The second method
Query the first page of data: Db.test.find (). Sort ({"Age": 1}). Limit (2);
Same as the first method above.
Query the data for the second page:
This is to get the value of the last record on the first page and then exclude the previous record and get the new record.
In summary, if the amount of data is not very large, you can use the first method, after all, relatively simple, if the amount of data is relatively large, use the second method is better, because this can not skip () This function, skip skip too many records, the efficiency is a little low