How to sort a collection by date in MongoDB?

后端 未结 10 2051
失恋的感觉
失恋的感觉 2020-12-07 14:27

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 col

10条回答
  •  感动是毒
    2020-12-07 14:55

    With mongoose it's as simple as:

    collection.find().sort('-date').exec(function(err, collectionItems) {
      // here's your code
    })
    

提交回复
热议问题