title: MongoDB REST API dynamic data pagination
body: <p>I am implementing REST API with Express and Mongoose. I am stuck with a task to paginate results for a collection with large number of documents. Document is a social media post which has fields like: <em>created_at</em>, <em>updated_at</em> and <em>score</em>.</p>
  
  <p>Ordinary paging implementation using limit and page number doesn't work because the nature of data. Posts can be removed or added while user is browsing the results.</p>
  
  <p>I could implement range-based pagination as suggested in MongoDB documentation.
  Last items in a page <em>created_at</em> value could be used to query next page.</p>
  
  <pre><code>Post.find({ created_at: { $gt: req.query.next } })
  </code></pre>
  
  <p>But what if I want to sort results by <em>score</em> which is not unique or all scores are the same?</p>