【发布时间】:2016-08-27 18:41:40
【问题描述】:
我正在尝试编写 MongoDB 查询,该查询将返回一小时前的数据。
有一列 time 带有时间戳 ("time" : NumberLong("1471953787012")),这就是它在 SQL 中的样子:
select name from table
where time between (NOW() - INTERVAL 1 HOUR) AND (NOW())
如何编写 MongoDB 查询以查找一小时前的日期范围?
我正在尝试使用 new Date() 函数,但它不起作用。
有谁知道我做错了什么?
db.coll.find({
"time" : {
$lt: new Date(),
$gte: new Date(new Date().setDate(new Date().getDate()-1))
}
})
【问题讨论】:
-
@ReyanChougle 不幸的是,我无法通过您提供的解决方案获得任何结果:(
-
我正在尝试
db.coll.find({$and:[{time:{$gte: new Date(ISODate().getTime() - 1000 * 60 * 60)}},{time: {$lte: ISODate()}}]}) -
另外,我尝试在另一个字段和日期范围上使用索引,例如:
db.coll.find({ Name: "Name1", time: {$gte: new Date(ISODate().getTime() - 1000 * 60 * 60), $lt: ISODate().getTime()}}); -
您找到解决方案了吗?正如我检查了我的帖子,它工作正常
-
我尝试执行查询,但无法从 mongo shell 获取结果。可能是日期格式的问题?这是一个 NumberLong
("time" : NumberLong("1471953787012"))
标签: mongodb datetime mongodb-query