在两个日期之间查找对象MongoDB

我一直在围绕在mongodb中存储推文,每一个对象看起来像这样: html

{
"_id" : ObjectId("4c02c58de500fe1be1000005"),
"contributors" : null,
"text" : "Hello world",
"user" : {
    "following" : null,
    "followers_count" : 5,
    "utc_offset" : null,
    "location" : "",
    "profile_text_color" : "000000",
    "friends_count" : 11,
    "profile_link_color" : "0000ff",
    "verified" : false,
    "protected" : false,
    "url" : null,
    "contributors_enabled" : false,
    "created_at" : "Sun May 30 18:47:06 +0000 2010",
    "geo_enabled" : false,
    "profile_sidebar_border_color" : "87bc44",
    "statuses_count" : 13,
    "favourites_count" : 0,
    "description" : "",
    "notifications" : null,
    "profile_background_tile" : false,
    "lang" : "en",
    "id" : 149978111,
    "time_zone" : null,
    "profile_sidebar_fill_color" : "e0ff92"
},
"geo" : null,
"coordinates" : null,
"in_reply_to_user_id" : 149183152,
"place" : null,
"created_at" : "Sun May 30 20:07:35 +0000 2010",
"source" : "web",
"in_reply_to_status_id" : {
    "floatApprox" : 15061797850
},
"truncated" : false,
"favorited" : false,
"id" : {
    "floatApprox" : 15061838001
}

我将如何编写查询来检查created_at并查找18:47到19:00之间的全部对象? 我是否须要更新文档,以便以特定格式存储日期? python


#1楼

我按照个人要求在此模型中尝试过,之后须要建立对象时,我须要存储一个日期,我想检索个人html文件中两个日期之间的全部记录(文档),我使用的是如下格式mm / dd / yyyy jquery

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>

    <script>
//jquery
    $(document).ready(function(){  
    $("#select_date").click(function() { 
    $.ajax({
    type: "post",
    url: "xxx", 
    datatype: "html",
    data: $("#period").serialize(),  
    success: function(data){
    alert(data);
    } ,//success

    }); //event triggered

    });//ajax
    });//jquery  
    </script>

    <title></title>
</head>

<body>
    <form id="period" name='period'>
        from <input id="selecteddate" name="selecteddate1" type="text"> to 
        <input id="select_date" type="button" value="selected">
    </form>
</body>
</html>

在个人py(python)文件中,我按照如下方式将其转换为“ iso fomate” web

date_str1   = request.POST["SelectedDate1"] 
SelectedDate1   = datetime.datetime.strptime(date_str1, '%m/%d/%Y').isoformat()

并以“ SelectedDate”做为个人收藏集中的字段保存在个人dbmongo收藏中 ajax

检索我在如下查询中使用的两个日期之间的数据或文档 mongodb

db.collection.find( "SelectedDate": {'$gte': SelectedDate1,'$lt': SelectedDate2}})

#2楼

将日期塞入Mongo时,将日期转换为GMT时区。 这样,永远不会存在时区问题。 而后,当您拉回数据进行演示时,只需对twitter / timezone字段进行数学计算便可。 mongoose


#3楼

澄清。 重要的是要知道: ide

  • 是的,您必须传递一个Javascript Date对象。
  • 是的,它必须是ISODate友好的
  • 是的,根据个人经验,您须要调整ISO日期
  • 是的,使用日期一般一般是一个乏味的过程,而mongo也不例外

这是一个有效的代码段,咱们在其中进行了一些日期操做以确保Mongo(这里我正在使用mongoose模块,并但愿其date属性小于(做为myDate参数指定的日期的)日期的行的结果)可以处理它正确: post

var inputDate = new Date(myDate.toISOString());
MyModel.find({
    'date': { $lte: inputDate }
})

#4楼

MongoDB Cookbook中 查询日期范围(特定月或日) 对此问题有很好的解释,可是下面是我本身尝试过的方法,它彷佛能够工做。 url

items.save({
    name: "example",
    created_at: ISODate("2010-04-30T00:00:00.000Z")
})
items.find({
    created_at: {
        $gte: ISODate("2010-04-29T00:00:00.000Z"),
        $lt: ISODate("2010-05-01T00:00:00.000Z")
    }
})
=> { "_id" : ObjectId("4c0791e2b9ec877893f3363b"), "name" : "example", "created_at" : "Sun May 30 2010 00:00:00 GMT+0300 (EEST)" }

根据个人实验,您将须要将日期序列化为MongoDB支持的格式,由于如下内容会带来不但愿的搜索结果。

items.save({
    name: "example",
    created_at: "Sun May 30 18.49:00 +0000 2010"
})
items.find({
    created_at: {
        $gte:"Mon May 30 18:47:00 +0000 2015",
        $lt: "Sun May 30 20:40:36 +0000 2010"
    }
})
=> { "_id" : ObjectId("4c079123b9ec877893f33638"), "name" : "example", "created_at" : "Sun May 30 18.49:00 +0000 2010" }

在第二个示例中,没有预期的结果,可是仍然有一个结果。 这是由于完成了基本的字符串比较。


#5楼

按照http://bsonspec.org/#/specification的规定,MongoDB实际上将日期的毫秒数存储为int(64)。

可是,当您检索日期时,它将变得很是混乱,由于客户端驱动程序将使用其本身的本地时区实例化日期对象。 mongo控制台中的JavaScript驱动程序确定会这样作。

所以,若是您关心本身的时区,请确保在知道时区时知道该怎么办。 对于查询来讲,这可有可无,由于它仍然等同于相同的int(64),不管您的日期对象位于哪一个时区(我但愿)。 可是我绝对会使用实际的日期对象(而不是字符串)进行查询,而后让驱动程序执行其操做。

相关文章
相关标签/搜索