在MongoDB中获取给定日期范围内的文档

在MongoDB中获取给定日期范围内的文档,mongodb,Mongodb,我在MongoDB中有一个名为goalsdata的数据库。有一个集合新的\u目标。文件看起来像 { "_id" : ObjectId("5b2d0f71ee19fb0021e0000f0"), "method" : "GET", "action" : "goals", "request_time" : ISODate("2018-06-21T15:02:09.784Z") }, { "_id" : ObjectId("5b2d0f71ee19fb00

我在MongoDB中有一个名为
goalsdata
的数据库。有一个集合
新的\u目标
。文件看起来像

{
    "_id" : ObjectId("5b2d0f71ee19fb0021e0000f0"),
    "method" : "GET",
    "action" : "goals",
    "request_time" : ISODate("2018-06-21T15:02:09.784Z")


},

{
    "_id" : ObjectId("5b2d0f71ee19fb0031e0000f0"),
    "method" : "GET",
    "action" : "goals",
    "request_time" : ISODate("2018-06-22T15:03:09.784Z")

},

{
    "_id" : ObjectId("5b2d0f71ee19fb0041e0000f0"),
    "method" : "GET",
    "action" : "goals",
    "request_time" : ISODate("2018-06-23T15:02:08.784Z")

},

{
    "_id" : ObjectId("6b2d0f71ee19fb001e0000f0"),
    "method" : "GET",
    "action" : "goals",
    "request_time" : ISODate("2018-06-24T15:02:07.784Z")

},

{
    "_id" : ObjectId("8b2d0f71ee19fb001e0000f0"),
    "method" : "GET",
    "action" : "countries_goals_exams",
    "request_time" : ISODate("2018-06-26T15:02:06.784Z")

}
有一个键叫做
请求时间
。我想查询并获取
request\u time
范围在
2018-06-19
2018-06-21
之间的文档

由于,
2018-06-19
请求时间
不存在。因此,它将考虑给定的范围并显示输出,

{
    "_id" : ObjectId("5b2d0f71ee19fb0021e0000f0"),
    "method" : "GET",
    "action" : "goals",
    "request_time" : ISODate("2018-06-21T15:02:09.784Z")


}
我可以使用这个查询从MongoDB中获取上一个小时前的数据

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

但是,我想使用key
request\u time
和范围来获取数据。推荐的方法是什么?

您只需相应地调整您的时间范围:

db.new_goals.find({
    request_time: {
        $gte: new Date('2018-06-19'), 
        $lt: new Date('2018-06-22')
    }
})