0
votes

If I have a collection, and collection contain several documents.

Every document contain field age

The id of collection is data

There are 20 documents.


db.collection('data').orderBy('age')

My question is:

How to get certain range of that documents which is already order by age.

Example:

After order by age

[doc5, doc12, doc9, doc4, doc1, doc15, doc7, doc14, doc11, doc17,

doc3, doc2, doc13, doc8, doc6, doc18, doc20, doc16, doc19, doc10]

  1. Fourth to Sixth (doc4, doc1, doc15)
  2. Eleventh to Twelfth (doc3, doc2)
  3. Fourteenth (doc8)
1
I mean first second third fourth..., not query by age. age is example. I don't know every document's age value. - deepL

1 Answers

1
votes

Firestore does not offer (actually, no longer offers) client-side queries that let you jump to an offset within the query results. You have to start from the beginning, and manually skip each result that you're not interested in, keeping track of the current index as you go. Yes, this will cost you excess document reads, but you don't have an alternative if you're not able to assign index values of your own for each document.

If you want to perform the query on a backend, you have offset() available, but the documents skipped are still counted as reads, and is neither an efficient or cheap way of skipping unwanted results.