Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I am using MongoDB with Node.JS. I have a collection which contains a date and other rows. The date is a JavaScript Date object.

How can I sort this collection by date?

2 Answers

0 votes
by (106k points)

You can use the below-mentioned code to sort a collection by date in MongoDB:-

collection.find().sort({datefield: -1}, function(err, cursor){...});

0 votes
by (107k points)

This worked for me. Using Node.js, Express.js, and Monk:

collection.find({}, {"sort" : [['datefield', 'asc']]}, function (err, docs) { ... });

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)
0 votes
2 answers
0 votes
2 answers
asked Oct 17, 2019 in Web Technology by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...