0

I am developing a pagination on a collection in mongodb using combination of skip and limit. in the express get route, i get params as page:2 or page:3 and so on. When its on page 2, i skip 10 records, on 3 20 records and so on.

But as per the mongodb documentation

The cursor.skip() method is often expensive because it requires the server to walk from the beginning of the collection or index to get the offset or skip position before beginning to return result. As offset (e.g. pageNumber above) increases, cursor.skip() will become slower and more CPU intensive. With larger collections, cursor.skip() may become IO bound.

Now this bothers me. It suggests range based navigation, but how do i actually do it in my case where i just get the page numbers. and how does it prevents this process to become IO bound?

Any detailed/explained answer?

beNerd
  • 3,314
  • 6
  • 54
  • 92
  • 1
    Here are some links to get you started: http://stackoverflow.com/questions/5525304/how-to-do-pagination-using-range-queries-in-mongodb http://stackoverflow.com/questions/8383488/how-to-implement-paging-in-mongodb http://stackoverflow.com/questions/6806063/range-based-paging-mongodb as for stopping IO bounding it is because skip will not use an index, i.e. it has to load the documents from the disk (possibly) whereas range based paging can use an index which is either in memory, or if not can easily be loaded. – Sammaye Mar 20 '13 at 09:10
  • 1
    not satisfied with these answers. The reason is that, they will not allow you to get a random numbered pagination mechanism. Suppose a user directly clicked on sixth page? What should be the best approach. code snippets may help :) – beNerd Mar 20 '13 at 10:03
  • Well yes, this is a problem in all applications which have to implement massive paging (by massive I mean skipping more than 100,000 rows, if your not doing that then ignore any advice I give) if you look at other apps, like facebook or Splunk or things like that you will notice they don't allow this for good reason, because it would kill their databases. At the end of the day your idea of paging has to change if your paging that much data – Sammaye Mar 20 '13 at 10:10

0 Answers0