mongoose:找到最新的文件 [英] mongoose: find most recent document
问题描述
我有mongoose架构,其日期属性只是
I have mongoose schema which has a day attribute which is just
Math.floor((new Date()).getTime() / (24 * 3600 * 1000))
我想查找数据最后一天输入所以今天说是 16085
然后我想找到输入的最后一天。或者另一种说法是这样的。什么是天
属性的文件少于 16085
,但是大于的所有其他文件一天
也小于 16085
。
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. 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
.
或其他方式
<$ c $所有文件集的最大元素是多少? c> day 属性少于 16085
其他然后迭代我的所有文件,我怎么能这样做?
Other then iterating over all of my documents, how could I do this?
推荐答案
在shell中它将是:
In the shell it would be:
db.test.find({day: {$lt: 16085}}).sort({day: -1}).limit(1)
查找天
小于16085的所有文档,在 day
降序,然后取第一个。
Which finds all the docs where day
is less than 16085, sorts them on day
descending, and then takes the first one.
在Mongoose中它会是这样的:
In Mongoose it would be something like:
MyModel.find({day: {$lt: 16085}}).sort({day: -1}).limit(1).exec((err, docs) => { ... });
这篇关于mongoose:找到最新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!