1

I want to sort a collection by the rating field and the _id field, both descending { $sort: { rating:-1 }, { _id:-1 } }. Since I'm using $limit I want to be able to do another request to get the following objects. I tried doing it with a $match like this { rating: {$lte: lastRating}, _id : { $lt: lastID} }, where lastRating and lastID are the previous values of the last item in the last returned array But that doesn't return anything.

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Paul
  • 776
  • 1
  • 5
  • 18

1 Answers1

3

What you are describing is called pagination.

How to implement this pattern is already described in depth by many others (like this). leaving this here for others

The reason your query does not return any documents on the next page, however, is probably simple because the tiebreaker is implemented incorrectly (I am assuming you are using the _id as tiebreaker).

It should look similar to this:

// page 1
collection
  .find({})
  .sort({ rating: -1, _id: -1 })
  .limit(limit)
  .toArray();
// page n
collection
  .find({
    $and: [
      { ...any actual query here },
      // this is the pagination query
      {
        $or: [
          { rating: { $lt: lastRating } },
          {
            rating: lastRating,
            _id: { $lt: ObjectId(lastID) } 
        ]
    ]
  })
  .sort({ rating: -1, _id: -1 })
  .limit(limit)
  .toArray();
MarcRo
  • 2,434
  • 1
  • 9
  • 24
  • thank you for this! The $or is clever for handling the tie-break situation. Exactly what I needed. – loeschg Feb 22 '23 at 22:09