mongoose: find most recent document

后端 未结 1 556
不思量自难忘°
不思量自难忘° 2020-12-19 20:11

I have mongoose schema which has a day attribute which is just

Math.floor((new Date()).getTime() / (24 * 3600 * 1000))

and I want to find

1条回答
  •  失恋的感觉
    2020-12-19 20:29

    In the shell it would be:

    db.test.find({day: {$lt: 16085}}).sort({day: -1}).limit(1)
    

    Which finds all the docs where day is less than 16085, sorts them on day descending, and then takes the first one.

    In Mongoose it would be something like:

    MyModel.find({day: {$lt: 16085}}).sort({day: -1}).limit(1).exec((err, docs) => { ... });
    

    0 讨论(0)
提交回复
热议问题