Back
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?
You can use the below-mentioned code to sort a collection by date in MongoDB:-
collection.find().sort({datefield: -1}, function(err, cursor){...});
This worked for me. Using Node.js, Express.js, and Monk:
collection.find({}, {"sort" : [['datefield', 'asc']]}, function (err, docs) { ... });
31k questions
32.8k answers
501 comments
693 users