Traditional Pagination Query
db.users
.find(conditions)
.sort({ _id: -1 })
.skip((page - 1) * numberOfPage)
.limit(numberOfPage);From the code above, this is a very traditional, unoptimized pagination query that retrieves data based on the page number and items per page. There's an obvious drawback: skip() traverses documents one by one. When the collection has a large amount of data, this becomes extremely slow.
Optimization
In typical pagination queries, the frontend maintains a page number variable as the key parameter for backend queries. But as discussed, this approach has significant downsides. Many optimizations involve using a unique value as the query condition with an index to speed up the query. The optimal choice is _id — as long as you have the _id of the last document from the previous query. This article focuses on this special MongoDB value.
db.users
.find(...conditions, {'$lt': _id})
.sort({_id: -1})
.limit(numberOfPage)ObjectID
I originally wrote quite a bit more, but then found someone had already written a better article explaining ObjectID, so I'll defer to that instead.
