Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda