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

139
Visualizações
Count A Specific Field In Firestore With JavaScript

I have a collection called users and one of the fields inside of the document has a string type called valid which the value can either be yes or no.

I am trying to create a query to show the number of documents where the valid field == yes and then show the total number. The following code I have just output yes yes to the console which is correct but I want it to show 2 to indicate there are 2 results.

db.collection('users').where('valid', '==', 'yes').get().then(snapshot => {

    snapshot.docs.forEach(doc => {

        console.log(doc.data().valid);
    });
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

QuerySnapshot class contains a field called size which returns:

The number of documents in the QuerySnapshot.

So in your code, that would simply be:

db.collection('users').where('valid', '==', 'yes').get().then(snapshot => {
    console.log(snap.size);
});

The result of this code will be:

2

But please note that will work for a small collection of documents. I wrote an article called:

  • How to count the number of documents in a Firestore collection?

Where I have explained a few alternative practices that can help you achieve the same thing, in a less expensive way.

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