How to sort a collection by date in MongoDB?

后端 未结 10 2032
失恋的感觉
失恋的感觉 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:48

    db.getCollection('').find({}).sort({_id:-1}) 
    

    This will sort your collection in descending order based on the date of insertion

提交回复
热议问题