title: How to query dates in MongoDB using $gte and $lte with flask-mongoengine?
body: <p>When I try to return documents based on the date created, I get an empty list when I know for a fact that there are documents in the database that meet the criteria. I used postman to send the request which would be a string input from the user eg. <code>"Tue Apr 28 2020"</code>. This string input would then be converted to a datetime object like so:</p>
  
  <pre><code>def get(self):
          try:
              body = request.get_json()
              search_field = datetime.datetime.strptime(body, '%a %b %d %Y') #format string to datetime object
  
              next_day = search_field
              next_day += relativedelta(days=1) #Set the end of the range to the next day
  
              search_field = search_field.replace(tzinfo=datetime.timezone.utc).isoformat()
              next_day = next_day.replace(tzinfo=datetime.timezone.utc).isoformat()
              print(search_field) #Verify the fields are correct : 2020-04-28T00:00:00+00:00
              print(next_day) #2020-04-29T00:00:00+00:00
  
              date_search = Reports.objects.filter(__raw__={'creation_timestamp' : {'$gte' : search_field, '$lte' : next_day}}).to_json() #This is where the documents should be filtered for return
              print(date_search)
  
              return Response(date_search, mimetype="application/json", status=200) #The document/s should be returned here as a JSON array.
  
          except Exception as e:
              print(e)
              return make_response(jsonify(message='Something went wrong :('), 401)
  </code></pre>
  
  <p>Here is the partial database model:</p>
  
  <pre><code>class Reports(db.Document):    
      creation_timestamp = db.DateTimeField(default=datetime.utcnow, required=True)
  </code></pre>
  
  <p>When the document is created, it is stored in the database and the time is stored as <code>isoformat()</code>. The user can only input the search field in the format stated above with a date picker so I format the date to fit the format Mongodb would understand. </p>
  
  <p>Using the above code, I get an empty list and the 200 status code. Checking the database shows I have documents that would fit the criteria, can anyone help figure out what is wrong? Thanks.</p>