Find objects between two dates MongoDB

后端 未结 14 1972
余生分开走
余生分开走 2020-11-21 06:03

I\'ve been playing around storing tweets inside mongodb, each object looks like this:

{
\"_id\" : ObjectId(\"4c02c58de500fe1be1000005\"),
\"contributors\" :          


        
相关标签:
14条回答
  • 2020-11-21 06:20

    MongoDB actually stores the millis of a date as an int(64), as prescribed by http://bsonspec.org/#/specification

    However, it can get pretty confusing when you retrieve dates as the client driver will instantiate a date object with its own local timezone. The JavaScript driver in the mongo console will certainly do this.

    So, if you care about your timezones, then make sure you know what it's supposed to be when you get it back. This shouldn't matter so much for the queries, as it will still equate to the same int(64), regardless of what timezone your date object is in (I hope). But I'd definitely make queries with actual date objects (not strings) and let the driver do its thing.

    0 讨论(0)
  • 2020-11-21 06:23

    use $gte and $lte to find between date data's in 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")}})
    
    0 讨论(0)
  • 2020-11-21 06:23

    i tried in this model as per my requirements i need to store a date when ever a object is created later i want to retrieve all the records (documents ) between two dates in my html file i was using the following format 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>
    

    in my py (python) file i converted it into "iso fomate" in following way

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

    and saved in my dbmongo collection with "SelectedDate" as field in my collection

    to retrieve data or documents between to 2 dates i used following query

    db.collection.find( "SelectedDate": {'$gte': SelectedDate1,'$lt': SelectedDate2}})
    
    0 讨论(0)
  • 2020-11-21 06:26

    Python and pymongo

    Finding objects between two dates in Python with pymongo in collection posts (based on the tutorial):

    from_date = datetime.datetime(2010, 12, 31, 12, 30, 30, 125000)
    to_date = datetime.datetime(2011, 12, 31, 12, 30, 30, 125000)
    
    for post in posts.find({"date": {"$gte": from_date, "$lt": to_date}}):
        print(post)
    

    Where {"$gte": from_date, "$lt": to_date} specifies the range in terms of datetime.datetime types.

    0 讨论(0)
  • 2020-11-21 06:26
    db.collection.find({"createdDate":{$gte:new ISODate("2017-04-14T23:59:59Z"),$lte:new ISODate("2017-04-15T23:59:59Z")}}).count();
    

    Replace collection with name of collection you want to execute query

    0 讨论(0)
  • 2020-11-21 06:28

    Convert your dates to GMT timezone as you're stuffing them into Mongo. That way there's never a timezone issue. Then just do the math on the twitter/timezone field when you pull the data back out for presentation.

    0 讨论(0)
提交回复
热议问题