我一直在mongodb中存储tweets,每个对象看起来是这样的:

{
"_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?我是否需要更新我的文档以使日期以特定的格式存储?


当前回答

Scala: 使用joda DateTime和BSON语法(reactivmongo):

val queryDateRangeForOneField = (start: DateTime, end: DateTime) =>
    BSONDocument(
      "created_at" -> BSONDocument(
        "$gte" -> BSONDateTime(start.millisOfDay().withMinimumValue().getMillis), 
        "$lte" -> BSONDateTime(end.millisOfDay().withMaximumValue().getMillis)),
     )

where msofday ().withMinimumValue() for“2021-09-08T06:42:51.697Z”将会是“2021-09-08T00:00:00.000Z” 而且 在millisOfDay()。withMaximumValue() for“2021-09-08t6:42:51.697 z”将会是“2021-09-08T23:59:99.999Z”

其他回答

我尝试在这个模型中,根据我的需求,我需要存储一个日期,当一个对象创建后,我想检索两个日期之间的所有记录(文档) 在HTML文件中 我使用以下格式mm/dd/yyyy

<!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” 以以下方式

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

并保存在我的dbmongo集合与“SelectedDate”字段在我的集合

检索数据或文档之间的2个日期,我使用以下查询

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

使用$gte和$lte在mongodb中查找日期数据

var tomorrowDate = moment(new Date()).add(1, 'days').format("YYYY-MM-DD");
db.collection.find({"plannedDeliveryDate":{ $gte: new Date(tomorrowDate +"T00:00:00.000Z"),$lte: new Date(tomorrowDate + "T23:59:59.999Z")}})
db.collection.find({"createdDate":{$gte:new ISODate("2017-04-14T23:59:59Z"),$lte:new ISODate("2017-04-15T23:59:59Z")}}).count();

将集合替换为要执行查询的集合的名称

Scala: 使用joda DateTime和BSON语法(reactivmongo):

val queryDateRangeForOneField = (start: DateTime, end: DateTime) =>
    BSONDocument(
      "created_at" -> BSONDocument(
        "$gte" -> BSONDateTime(start.millisOfDay().withMinimumValue().getMillis), 
        "$lte" -> BSONDateTime(end.millisOfDay().withMaximumValue().getMillis)),
     )

where msofday ().withMinimumValue() for“2021-09-08T06:42:51.697Z”将会是“2021-09-08T00:00:00.000Z” 而且 在millisOfDay()。withMaximumValue() for“2021-09-08t6:42:51.697 z”将会是“2021-09-08T23:59:99.999Z”

澄清。重要的是要知道:

是的,你必须传递一个Javascript Date对象。 是的,它必须是ISODate友好的 是的,根据我的经验,让这个工作,你需要操纵日期到ISO 是的,处理日期通常是一个乏味的过程,芒果也不例外

下面是一个工作代码片段,在这里我们做了一点日期操作,以确保Mongo(这里我使用的是mongoose模块,并希望结果的行,其日期属性小于(之前)给出的日期myDate参数)可以正确处理:

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