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

179
Visualizações
get length of all documents in collection mongoose

I am trying to get the count for how many documents I have in my collection. I have this, but it is not returning what I need it to, it is returning a whole bunch of unnecessary info I don't need for this simple task:

var estimatedDocumentCount = ServicesModel.countDocuments({});
console.log(estimatedDocumentCount)

It is returning the entire query, plus all its embedded parameters it seems like. how do I do this properly?

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

0

async function countDocuments() {
  const count = await ServicesModel.countDocuments({});
  return count;
};

const count = countDocuments();
about 4 years ago · Juan Pablo Isaza Relatório

0

It's probably because countDocuments is an asynchronous call and you are executing it synchronously.

Follow the syntax mentioned in Mongoose Docs which uses a callback function to get the count.

ServicesModel.countDocuments({}, function (err, count) {
  console.log(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