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

735
Visualizações
How To Count Number of Documents in a Collection in Firebase Firestore With a WHERE query in react.js

I want to get the total number of users in my Firebase Firestore collection. I can get it easily by writing this code:

const [totalUsers, setTotalUsers] = useState(0);
  const [newUsers, setNewUsers] = useState(0);

  useEffect(() => {
    firebase.firestore().collection("Users").get().then((querySnapshot) => {
      const TotalUsers = querySnapshot.size
        setTotalUsers(TotalUsers)

    })
  }, []);

But what i want is to get the total number of users with a condition such as the following:

   // this is not working, its not showing any results back.
  useEffect(() => {
    firebase.firestore().collection("Users").where("usersID","!=","101010").get().then((querySnapshot) => {

      querySnapshot.forEach((doc) => {
        const TotalUsers = doc.size
        setTotalUsers(TotalUsers)
      })
      

    })
  }, []);

But the above code is not working and isn't returning any results.

How can I get the total number of documents in a collection in firebase firestore with a where query?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You've moved the count operation into a loop, which is not needed.

Instead, just add the condition to your read operation and keep the rest the same:

useEffect(() => {
  firebase.firestore().collection("Users").where("usersID","!=","101010").get().then((querySnapshot) => {
    const TotalUsers = querySnapshot.size
    setTotalUsers(TotalUsers)
  })
}, []);

Note that reading all user documents just to determine their count is an expensive way to do this, so I recommend reading the documentation on aggregation operators, and Dan's answer on counting documents in Cloud Firestore collection count

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