Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

0

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

const count = countDocuments();
about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!