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

333
Views
MongoServerError: el valor límite no debe ser negativo

Estoy atascado cuando recibo un error como este, he estado buscando una solución en todas partes pero no he obtenido ningún resultado. Por favor, ayúdame. gracias

Product.js "Rutas"

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

Producto.js "Controlador"

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

Mi error:

ingrese la descripción de la imagen aquí

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

0

Una mejor solución es usar "countDocuments" para obtener el conteo,

Puedes usar esto:

 const count = await Product.countDocuments(query);

Ejemplos:

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

O

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

Puede usar la siguiente agregación en mongodb 3.6

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

con salto y límite

 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!