Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

339
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda