首页 > 解决方案 > Mongoose 查询给定日期的 mongodb

问题描述

我已经尝试了几个选项来检索给定日期的记录,特别是今天的日期,如下所示。我仍然得到0条记录。

 const coins = await Coin.find({
  lastVoted: {
    $gte: new Date(2021, 7, 5),
    $lt: new Date(2021, 7, 7),
  },
})
 const coins = await Coin.find({
      approved: true,
      lastVoted: new Date(2021, 7, 7),
      
    })

标签: mongodbmongoose

解决方案


在做了一些研究之后,如果发现date-fns可以正常工作。我就是这样做的

const timeOfDay = require('date-fns')
const coins = await Coin.find({
      
      lastVoted: {
        $gte: timeOfDay.startOfDay(new Date()),
        $lt: timeOfDay.endOfDay(new Date()),
      },
    })

推荐阅读