简体   繁体   English

mongoose:找到最新的文件

[英]mongoose: find most recent document

I have mongoose schema which has a day attribute which is just 我有mongoose架构,它有一个day属性

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

and I want to find the data for the last day entered so say today is 16085 then I want to find the last day entered. 我想找到输入的最后一天的数据所以说今天是16085然后我想找到输入的最后一天。 Or another way of saying this would be. 或者另一种说法是这样的。 What is the document with a day attribute less then 16085 but greater then all other documents where day is also less then 16085 . 什么是day属性小于16085文档,但大于所有其他文档的day也小于16085

or another way 或另一种方式

What is the maximal element of the set of all documents with a day attribute less then 16085 day属性小于16085的所有文档集的最大元素是什么

Other then iterating over all of my documents, how could I do this? 然后迭代我的所有文档,我怎么能这样做?

In the shell it would be: 在shell中它将是:

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. 其中发现所有的文档,其中day低于16085,排序他们day降下来,然后采取的第一个。

In Mongoose it would be something like: 在Mongoose中它会是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM