Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How to return the total number of results on a paginated Firestore query without paying for all the reads

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:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Apparently this is not possible.

If you query the whole thing to know the total, you are paying for all those reads. That's a fact.

Just re-watched their official tutorial video about pagination, and there's a bunch of people in the comments complaining exactly about the lack of this feature.

You could set up Cloud Functions (with Firestore triggers) to keep track of your query sizes if you know the filters (where clause) your are going to use, but that should be cumbersome for sure.

If you are going that route, you might also just go ahead and set up a full-blown search service like ElasticSearch, Algolia or something.

Their official tutorial video on Firestore pagination:

https://www.youtube.com/watch?v=poqTHxtDXwU

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!