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

695
Views
How can I sort and limit with Mongoose

I made an review app with Express and Mongoose. I have an review model like below:

var mongoose = require('mongoose');

var ReviewSchema = mongoose.Schema({
    title: String,
    description: String,
    rating: Number
}, {
    timestamps: true
}
);

module.exports = mongoose.model('Review', ReviewSchema);

In my controller I just get all reviews list as below. But now I want to get a list with 10 recently reviews & sort by (orderby timestamps). How can I do it with mongoose? Please help me! I am a newbie with NodeJS and 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);
        }
    });
};

Thanks you so much

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This should work for you:

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

0

you can try like this:

 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!