0

Using suggestions from here and here, I'm trying to implement a scalable, paginating, stateless REST API in the following way:

//Page 1
db.users.find().limit(pageSize);
//Find the id of the last document in this page
last_id = ...

//Page 2
users = db.users.find({'_id'> last_id}).limit(pageSize);
//Update the last id with the id of the last document in this page
last_id = ...

It works in most cases, but the problem is I have to sort a list of videos according to view count and then try to paginate.

// Page 2
videos = db.videos.find({'viewCount'< last_view_count}).sort({viewCount : 'descending'}).limit(pageSize);

This does not work since there might be multiple items with same view count and some items will miss out when we do 'viewCount'< last_view_count

Any ideas on how to solve this problem would be appreciated :)

Community
  • 1
  • 1
Harsha Bhat
  • 718
  • 10
  • 25
  • Possible duplicate of [Calculate skip value for given record for sorted paging](http://stackoverflow.com/questions/31242867/calculate-skip-value-for-given-record-for-sorted-paging) – Blakes Seven Nov 24 '15 at 04:36
  • Is there any way to achieve this in a state-less environment where each request can hit any of the servers in a cluster and run in isolation? – Harsha Bhat Nov 24 '15 at 04:58
  • A session store or any kind of persistence is generally required for stateless requests. If you are not using cookie based sessions then you should be using a token in the request. The bottom line is the persisted store needs to be accessible to all worker nodes. That is a common practice in high-availability systems. – Blakes Seven Nov 24 '15 at 05:00

1 Answers1

4

You can sort and filter on a combination of fields that together uniquely identify documents. For example, sort on {viewcount: 'descending', _id: 'ascending'}, and filter on {viewcount <= last_view_count, _id > last_id}.

weiyin
  • 6,819
  • 4
  • 47
  • 58
  • Good answer. Just wondering, is there any way to extend this to a two-way backward/forward pagination? Ex. being able to get both page 2 and page 4 while having the page 3 response? – Harsha Bhat Dec 11 '15 at 06:29