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

364
Vistas
Mongoose query always returns empty array

Im trying to make a greater than query but it always returns an empty array.

    const productSchema = require("./productsSchema");
const getProductsGreaterThan = async (minimum_id) => {
try{
    console.log(minimum_id);
    const productById = await productSchema.find({id: {$gt:minimum_id}});
    return productById
}
catch(err){
    console.error(err);
}
}

This is my controller:

if (req.params.minimum_id) {
    const productsById = await productsModel.getProductsGreaterThan(req.params.minimum_id);
    res.status(200).json(productsById);
}

It should return the products with an ID greater than what i type in the req.params...

routes.get("/products/:minimum_id?",productRoutes.getProductsGreaterThan);

But when i write the route like this in the url it gives me an empty array => http://localhost:3000/products/10 What is wrong with this code? Im very confused, because other queries like find({}) work. My mongoose version is 6.2.10.

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

If you are using mongoose and MongoDB the correct id format is _id. So you would do the following:

productSchema.find({_id: {$gt:minimum_id}})
about 4 years ago · Santiago Gelvez Denunciar

0

Solved. Somehow the problem was in the name of the collection of mongoDB. It seems it should be always in lower-case and plural.

about 4 years ago · Santiago Gelvez 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