Find objects between two dates MongoDB

后端 未结 14 2052
余生分开走
余生分开走 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: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

    
    
    
    
    
        
    
        
    
    
    
        
    from to

    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}})
    

提交回复
热议问题