relay-mongodb-connection
relay-mongodb-connection copied to clipboard
Offset as cursor?
When using a simple offset/skip as the cursor, won't pagination break during mutations?
eg.
- get the first page (5 records), ordered by latest / desc
- mutation creates a new record
- 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?
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.