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

173
Vistas
How to send a sorted result from my MongoDB database by clicking on a button?

I have this code where i send my collection to index.ejs

router.get('/', (req, res) =>{
    FILM
        .find().limit(6)
        .then(films => res.render('index.ejs', {films}))
})

In index.ejs I want to sort my collection by rating from button. What way can I do it?

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

0

Try this:

router.get('/', (req, res) =>{
    var sortFilter = { rating : 1 };
    FILM.find().sort(sortFilter).limit(6)
        .then(films => res.render('index.ejs', {films}))
})

Please refer docs for more information

about 4 years ago · Santiago Trujillo Denunciar

0

You can add in index.ejs an anchor tag that would send a request to / with a url param, something like this for example:

<a href="/?sort=1">Sort</a>

You change you request handler, so that when there is a query parameter, it send a sorted list, otherwise send a normal one.

router.get("/", (req, res) => {
  const sort = req.query.sort;
  if (sort) {
    FILM.find()
      .sort({ rating: 1 })
      .limit(6)
      .then((films) => res.render("index.ejs", { films }));
  } else {
    FILM.find()
      .limit(6)
      .then((films) => res.render("index.ejs", { films }));
  }
});
about 4 years ago · Santiago Trujillo 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