简体   繁体   English

NodeJS MongoDB查找早于当前日期的帖子

[英]NodeJS MongoDB Find posts that are older than current date

So I have this code line here: 所以我在这里有此代码行:

let advertisements = await Advertisement.find({created_at: {$lt: moment().valueOf()}});

This is what it does: 这是它的作用:

created_at : returns a timestamp when post was created at: 1551198203488.0 created_at :在以下位置创建帖子时返回时间戳:1551198203488.0

moment.valueOf() return me a current timestamp for example: 1551198203488.0 moment.valueOf()返回当前时间戳,例如:1551198203488.0

Now I need to write this code line that it only finds ads that are 1 hour old. 现在,我需要编写此代码行,使其仅查找1小时以内的ads Is is possible somehow ? 有可能吗?

Also I storing and group2_date which store current timestamp BUT with 1 hour added to it like this: moment().add(1, 'h') 我还存储和group2_date ,其中存储了当前时间戳BUT, group2_date其中添加了1小时,例如: moment().add(1, 'h')

You can subtract one hour from the moment using .subtract function and then use $gte operator to obtain only the greater values/documents from the subtracted hours and less then from the current time. 您可以使用.subtract函数从该moment减去一小时,然后使用$gte运算符从所减去的小时数中仅获取较大的值/文档,而从当前时间中获取的则更少。

let advertisements = await Advertisement.find({
  "created_at": {
    "$lte": moment().toDate(),
    "$gte": moment().subtract(1, 'hours').toDate()
  }
})

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

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