【问题标题】:Returns Data IF date range contains timestamp如果日期范围包含时间戳,则返回数据
【发布时间】:2021-08-30 22:57:09
【问题描述】:

我正在使用 Nodejs、Express 和 mongoose 我正在尝试做的事情如下。
我有这个格式的对象:

{
  _id: 60c824cfeaaa84a7b4e16dbf,
  ActionID: 5f2715c9569c9700523bbcc3,
  ActionType: 'PushToBrand',
  EndDate: 2021-06-27T00:54:00.000Z,
  StartDate: 2021-06-15T00:54:00.000Z,
  ImageName: 'h21fTvVniZzqVAkY9oPJDj.jpg',
  ImagePlace: 1
},  
{
  _id: 60c824cfeaaa84a7b4e16sxf,
  ActionID: 5f2715c9569c9700523bbcc3,
  ActionType: 'PushToBrand',
  EndDate: 2021-05-27T00:54:00.000Z,
  StartDate: 2021-05-15T00:54:00.000Z,
  ImageName: 'h21fTvVniZzqVAkY9oPJDj.jpg',
  ImagePlace: 2
}

因此,一旦收到 TimsStamp 参数,我需要在相应的 StartDate 和 EndDate 范围内过滤确实包含 TimeStamp 的对象。

可能是这样的:

BolBanners.find({StartDate: Is equal or greater than Date.now() && EndDate:_IS equal or minor than Date.now()});

***更新

DB Compas 图片 enter image description here

【问题讨论】:

  • 您可以使用$lte$gte 运算符并使用new Date() 函数。
  • 我试过这个const validImages = BolBanners.find({StartDate: {$lte: Date.now()}, EndDate: {$gte: Date.now()}}); ,但没有用。
  • 见工作playground。使用new Date()。并确保字段 StartDate 和 EndDate 的日期类型都不是字符串。
  • 谢谢!我想我得到了 clores,但尽管 StartDate 和 EndDate 是日期格式,但查询也不能正常运行@turvishal
  • @turivishal 感谢您的合作,不幸的是我错过了一些东西。这是我的收藏文档 [i.stack.imgur.com/PajH4.jpg] 还有其他建议吗??

标签: node.js express date mongoose


【解决方案1】:

好的,@turivishal 提供的解决方案是正确的。问题是我的,因为我忽略了一些东西,我没有发布它,因为我没有看到完整的图片。

我是这样查询的:

var validImages;
try {
    validImages = BolBanners.find({
        StartDate: {
            $lte: new Date()
        },
        EndDate: {
            $gte: new Date()
        }
    });
    console.log(validImages)
    res.send(validImages);
} catch (e) {
    console.log('error')
    res.json({msg: 'no response'})
}});

但我错过了著名的等待,因为当您调用数据库时它是一个异步函数。

感谢您的指导。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2011-04-27
    相关资源
    最近更新 更多