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

152
Vistas
Mongo DB Search and sort not working - collection.find(...).sort is not a function

I have a use case to search for a particular string on one field and return the results in sorted order based on another field. The below is the function I'm using in Node.js and the error being thrown.

router.get("/getappts/:username", function (req, res) {
  console.log(req.params.username)
  collection.find({ username: req.params.username }).sort( {date : 1} ), function (err, appointments) {
    if (err) throw err;
    
    console.log(appointments)
    res.json(appointments);
  } 
})

Error - collection.find(...).sort is not a function.

Not sure how to model the query.

But the below query on Mongo Compass seems to work fine -

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

might be where your callback is, I would suggest using promises instead

router.get("/getappts/:username", async function (req, res) {
  try {
    console.log(req.params.username)
    const appointments = await collection.find({ username: req.params.username }).sort({ date : 1 })
    
    console.log(appointments)
    res.json(appointments);
  } catch (err) {
    throw err;
  }
}
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