-2

var query = { "to": req.params.id }; var mysort = { receivedDate: 1 };

Message.find(query, (err, doc) => {
    if (!err) {
        res.json(doc);
    } else {
        res.json(err);
    }
});
  • 3
    Does this answer your question? [How to sort a collection by date in MongoDB?](https://stackoverflow.com/questions/13847766/how-to-sort-a-collection-by-date-in-mongodb) – whoami - fakeFaceTrueSoul Apr 18 '20 at 17:13

1 Answers1

0

The syntax is like the following:

db.collecttionName.find().sort({date:1});

But instead of date, you can pass in other criteria.

Algo7
  • 2,122
  • 1
  • 8
  • 19