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

340
Views
MongoServerError: Limit value must be non-negative

I'm stuck when I get an error like this, I've been looking everywhere for a solution but haven't gotten any results. Please help me. thank you

Product.js "Routes"

router.get("/products/total/", productsCount)

Product.js "Controller"

exports.productsCount = async (req, res) => {
  let total = await Product.find({}).limit(0).estimatedDocumentCount().exec();
  res.json(total);
}

My error:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A better solution is to use "countDocuments" to get the count,

You can use this:

const count = await Product.countDocuments(query);

Examples:

const count = await Product.countDocuments({ categoryId : 'TEST' });

Or

Product.countDocuments({ categoryId : 'TEST' }, function (err, count) {
  console.log('Products count = ', count);
});

You can use below aggregation in mongodb 3.6

const result = await Product.aggregate([
  { "$facet": {
    "totalData": [
      { "$match": { categoryId : 'TEST' }}
    ],
    "totalCount": [
      { "$count": "count" }
    ]
  }}
])

with skip and limit

const result = await Product.aggregate([
  { "$facet": {
    "totalData": [
      { "$match": { categoryId : 'TEST' }},
      { "$skip": 10 },
      { "$limit": 10 }
    ],
    "totalCount": [
      { "$count": "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!