mongodb - 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),
})
解决方案
在做了一些研究之后,如果发现date-fns可以正常工作。我就是这样做的
const timeOfDay = require('date-fns')
const coins = await Coin.find({
lastVoted: {
$gte: timeOfDay.startOfDay(new Date()),
$lt: timeOfDay.endOfDay(new Date()),
},
})
推荐阅读
- r - 如何根据 ggplot2 绘制的图上的 y 轴值覆盖小平面标签?
- android - 如何计算 ViewPager2 中发生的滑动
- c++ - MsgBox 事件循环和 .show()
- angular - Angular Guard 中的 Http 调用以验证 JWT
- python - TypeError: filter 必须是 dict、bson.son.SON 或任何其他继承自 collections.Mapping 的类型的实例
- excel - Excel 搜索、查找、复制粘贴宏
- security - 如何将 Nessus 用于内部网络?
- html - 无法完全缩放图像,并使用溢出:隐藏工作
- javascript - Javascript:如何在午夜刷新页面?
- vba - 无法从图像链接数组中抓取第一个图像链接