I need to make a Firestore query in a collection that:
- is ordered by field "timestamp"
- the field "uid" is equal to any of the elements of a strings array (more than 10)
- I need to paginate the query so that I'm able to perform later more queries starting from a precise timestamp
The code should look like this:
Firestore.firestore()
.where(field: "uid", in: arrayOfUids)
.order(by: "timestamp", descending: false)
.limit(to: 5)
or in case of starting from a particular timestamp
Firestore.firestore()
.where(field: "uid", in: arrayOfUids)
.startAt("timestmap", lastTimestampFetched)
.order(by: "timestamp", descending: false)
.limit(to: 5)
How can I achieve this result? In this topic was suggested to perform multiple calls filtering with up to 10 elements in the array in each call (the maximum number of array elements for Firestore to be able to compare) but I didn't have the constraint of ordering by timestamp.
EDIT: I cannot order the query result client-side, it wouldn't be helpful for my use case. Let's say that we have 30 uids to query for: First, we have to divide these uids into groups of 10.
So we'll have 3 groups of 10 uids each (the max array lenght to for querying with Firestore); for each of this groups we'll do a separate query which looks like this:
Firestore.firestore()
.where(field: "uid", in: arrayOfTenUids)
.startAt("timestmap", lastTimestampFetched)
.order(by: "timestamp", descending: false)
.limit(to: 5)
For each query, we save the last document timestamp in order to be able to perform successive queries with pagination.
Here is the problem, we are going to have multiple last timestamps, since we are performing multiple queries, which timestamp are we going to save?
1st option: the last timestamp of all last timestamps
2nd option: the first timestamp of all last timestamps
1st case -> in successive queries we are going to fetch some documents that have been previously fetched
2nd case -> in successive queries we are going to miss some documents that have been previously fetched (because the last timestamp of one query could be after the last timestamp of another one)
This topic is really hard to explain but I did my best.