Categories
JavaScript Answers

How to get the latest and oldest record in Node Mongoose.js?

Spread the love

To get the latest and oldest record in Node Mongoose.js, we use the sort method.

For instance, we write

Tweet.findOne()
  .sort("-created_at")
  .exec((err, post) => {
    //...
  });

to call sort to sort the results by the created_at field in descending order.

Then we get the latest result from post in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *