简体   繁体   中英

How to query dates in MongoDB using $gte and $lte with flask-mongoengine?

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. "Tue Apr 28 2020" . This string input would then be converted to a datetime object like so:

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)

Here is the partial database model:

class Reports(db.Document):    
    creation_timestamp = db.DateTimeField(default=datetime.utcnow, required=True)

When the document is created, it is stored in the database and the time is stored as isoformat() . 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.

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.

If you can have your search_field and nextday in datetime format then you can write the query. I also suggest using Q for pymongo queries in mongoengine. Your query:

import Q from mongoengine
search_time=datetime.datetime(2017, 11, 8)
nextday=datetime.datetime(2017, 11, 9)
date_search=Report.objects(Q(creation_timestamp__gte=search_field) & Q(timestamp__lte=nextday)).to_json()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM