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

341
Visualizações
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 Respostas
Responde à pergunta

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 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