relay-mongodb-connection icon indicating copy to clipboard operation
relay-mongodb-connection copied to clipboard

Offset as cursor?

Open at0g opened this issue 8 years ago • 1 comments

When using a simple offset/skip as the cursor, won't pagination break during mutations?

eg.

  1. get the first page (5 records), ordered by latest / desc
  2. mutation creates a new record
  3. request the second page, ordered by latest / desc (skips 5 records)

Correct me if I've got this wrong, but won't the first record in step 3 be a duplicate of the last record from step 1?

at0g avatar Nov 15 '16 07:11 at0g

That and performance, are the drawbacks of skip because it has to walk though all the documents (or index values) from 0 to the skip value.


Possible alternative to skip would be to use ranged query, a'la:

// First query
const posts = Posts.find({}).limit(5).sort(uniqueValue: 1)

// Get the uniqueValue of LAST document
const last = posts[posts.length - 1].uniqueValue

// Get next 5 after the last document of previous query
const posts = Posts.find({uniqueValue: {$lt: last}}).limit(5).sort(uniqueValue: 1)

Bad parts:

  • uniqueValue has to be unique, can't sort by general values
  • It probably gets really nasty if you have several values to sort by

Not sure though, Im not a MongoDB pro, maybe there are workarounds (compound indexes?).

Good parts:

  • Doesn't really matter how big "offset" is, same performance
  • Mutations doesn't break pagination out of the box

This requires some input from MongoDB veterans.

adjourn avatar Nov 18 '16 09:11 adjourn