I'm working on a Firestore paginated query. Therefore, I would like to return the total number of results, so I can calculate the total number of pages and show a proper page navigation element.
You'll see from the docs below that they recommend you to use the last document on the queryResult to get the next page using startAfter(lastDoc)
. That's ok.
But how can I know the total number of results? Let's say I have 10.000 results in that query.
If I do:
const q = query(collection(db, "cities"), orderBy("population")); // WITHOUT THE limit
const querySnapshot = await getDocs(q);
const total = querySnapshot.docs.length; // I CAN READ THE TOTAL HERE
By querying the whole thing, I'm already paying for the 10.000 reads, right? Even though my user might only want to see the 1st page with 50 results, for example.
Is there a way to know the size/total of a query result, without paying for all the reads?
From Firestore docs: