MongoDB过去一小时的日期范围查询 [英] MongoDB Date range query for past hour

查看:1124
本文介绍了MongoDB过去一小时的日期范围查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写MongoDB查询,该查询将返回一个小时前的数据。
有一个带有时间戳的 time 列( time:NumberLong( 1471953787012))和在SQL中是这样的:

I'm trying to write MongoDB query which will be return data from one hour ago. There is a column time with timestamps ("time" : NumberLong("1471953787012")) and this is how it looks in SQL:

select name from table
where time between (NOW() - INTERVAL 1 HOUR) AND (NOW())

我如何编写MongoDB查询来查找日期范围一个小时前?
我正在尝试使用 new Date()函数,但是它不起作用。
有人知道我在做什么吗?

How do I write a MongoDB query to find a date range from one hour ago? I'm trying with new Date() function but it doesn't work. Does anyone know what I'm doing wrong?

db.coll.find({ 
  "time" : { 
    $lt: new Date(), 
    $gte: new Date(new Date().setDate(new Date().getDate()-1))
  }   
})


推荐答案

db.entity.find({ $and:[
    {
    "timestamp": {
        $gte: new Date(ISODate().getTime() - 1000 * 60 * 60)
    }}, 
    { 
    "timestamp": {
        $lte: ISODate()
    }}
]})

希望这会有所帮助...

Hope this helps...

这篇关于MongoDB过去一小时的日期范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆