Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda