Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

700
Views
¿Cómo puedo ordenar y limitar con Mongoose?

Hice una aplicación de revisión con Express y Mongoose. Tengo un modelo de revisión como el siguiente:

 var mongoose = require('mongoose'); var ReviewSchema = mongoose.Schema({ title: String, description: String, rating: Number }, { timestamps: true } ); module.exports = mongoose.model('Review', ReviewSchema);

En mi controlador, solo obtengo la lista de todas las revisiones como se muestra a continuación. Pero ahora quiero obtener una lista con 10 reseñas recientes y ordenarlas por (ordenar por marcas de tiempo). ¿Cómo puedo hacerlo con mangosta? ¡Por favor, ayúdame! Soy un novato con NodeJS y Mongodb.

 exports.findAll = function(req, res) { console.log("Fetching Review...") // Retrieve and return all reviews from the database. Review.find(function(err, reviews){ if(err) { console.log(err); res.status(500).send({message: "Some error occurred while retrieving Review."}); } else { res.send(reviews); } }); };

Muchas gracias

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esto debería funcionar para usted:

 Review.find() .sort({_id: -1}) .limit(10) .then(reviews => { console.log(reviews) });
over 4 years ago · Santiago Trujillo Report

0

puedes probar asi:

 Review.find({}, function(err,reviews){}).sort({_id: -1}).limit(10);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!