If you want to get all new things in the past 5 minutes you would have to do some calculations, but its not hard...
第一,就你想要匹配的财产编制一个指数(包括降水方向1和降水指标1)。
db.things.createIndex({ createdAt: -1 }) // descending order on .createdAt
然后询问过去5分钟的文件(60秒* 5分钟)。
db.things.find({
createdAt: {
$gte: new Date(new Date().getTime()-60*5*1000).toISOString()
}
})
.count()
<代码>新日期(新日期(新日期)-60*5*1000)。
第一,我们计算“5分钟前”:
new Date().getTime()
gives us current time in milliseconds
- We want to subtract 5 minutes (in ms) from that:
5*60*1000
-- I just multiply by 60
seconds so its easy to change. I can just change 5
to 120
if I want 2 hours (120 minutes).
new Date().getTime()-60*5*1000
gives us 1484383878676
(5 minutes ago in ms)
现在,我们需要将这一数据输入<代码>新日期()的构造者,以获得MongoDB时序所需的ISO扼制格式。
{ $gte: new Date(resultFromAbove).toISOString() }
(mongodb .find() query)
- Since we can t have variables we do it all in one shot:
new Date(new Date().getTime()-60*5*1000)
- ...then convert to ISO string:
.toISOString()
new Date(new Date().getTime()-60*5*1000).toISOString()
gives us 2017-01-14T08:53:17.586Z
当然,如果你重新使用权宜之计的驱动力,这比变数小得多,但是,这在通常用于检查情况的省会中进行。