calviners
calviners

Reputation: 137

MongoDB - Paging with ranged queries

I am currently working on a web application with MongoDB, and it populates the browser with pages of content.

I am aware of the two methods of paging (seen here: https://scalegrid.io/blog/fast-paging-with-mongodb/):

1) .skip() + .limit()

2) .find({_id > last_id}).limit(n)

I understand the performance issues with the first solution, so that option was out of the picture.

The second solution works great, but only if the results are sorted by _id. If you sort by a non-unique field such as a date, there can be more than one document with that date so some documents may be excluded from the results.

Also, the user must be navigating through the pages linearly because there is a last_id variable that constantly needs to be updated and is determined by the _id of the last document from the previous page. If the user were to jump from page 1 to 7, page 7 would show the results meant for page 2 because the last_id would hold the _id from the last document of page 1.

So my question is, how would one implement paging if we were sorting by a non-unique field (such as a Date or a firstName), and how would we allow users to jump multiple pages at once?

Any tips would be greatly appreciate, thank you!

Upvotes: 0

Views: 68

Answers (1)

HMR
HMR

Reputation: 39270

The comment is correct, you should be fine with skip unless you know you're going to have a lot of documents. But even if you don't expect much but then get enormously successful/popular you don't want your code to break just is it gets used by millions of users.

If you sort by date and then by id you can do the following:

.find(
  {
    date:{
      $lte:lastDate
    },
    _id:{ 
      $lt: lastId 
    }
  }
).limit(n)

Upvotes: 1

Related Questions