How can I paginate Firestore results between a lower and upper bound using query cursors without fetching previous pages?
0 reputation · 25 Dec 2020, 14:00 UTC
I need to retrieve a specific page of results from a Firestore collection ordered by population, while restricting the results to a defined lower and upper population bound (e.g., between 1,000,000 and 5,000,000). The collection is large and frequently updated, so I want to avoid reading all preceding pages to reach the desired page. I plan to use query cursors (startAt/startAfter and endAt/endBefore) combined with limit() to paginate, but I am unsure how to compute the cursors for an arbitrary page without iterating through earlier batches.
How can I derive a start cursor for page N directly using document snapshots that satisfy the bound? Is it safe to reuse the same end cursor for all pages, or must it be recomputed when data changes? What are the consistency guarantees if documents are added or removed between queries?