1 What is paging in mongodb
It is to return several consecutive rows in the table at a time.
2 What is sql pagination
The same is to return several consecutive rows in the table.
3 How to implement sql paging
利用order by xxx limit xxx
4 How to implement mongodb paging
First, use skip and limit
db.test.sort({"idx":1}).skip(10000).limit(10);
skip needs to traverse the linked list. When the skip data is large, the speed is very slow, so skip is not recommended at this time. Instead use query.
Second, use the query
db.test.find({idx:{$gt:10000}}).sort({"idx":1}).limit(10);
Here, first query, go to index, then sort the results, and then go to the top 10.
5 Usage scenarios of database paging
For example, in a news app, there are 10,000 pieces of news in the database. If it is displayed, each user swipe will trigger a query, and each query will return 20 pieces of data. Then these 20 pieces of data are one page, and each time the database is queried, these 20 pieces of data will be returned. Just do it.