0

I wish to select a range of dates using query, but it looks like I have the timezone formatted dates, however I thought these were wrapped as the date object so that I can use the date object in python/pymongo. However when I use the query, I do not get what I expect. I tried also converting date to ISODate. So my question is, should I:

a) Reload all the date data in a different format. b) Work with the dates in mongo as they are.

Below I'm stringing together multiple options, hence the or, but for the case I have for example:

query = {}
query["$or"] = []
query["$or"].append({"date_object": {"$lte": time_to.isoformat(), "$gte": time_from.isoformat() } })

giving:

query = {'platform': {'$in': [re.compile('windows', re.IGNORECASE)]}, '$or': [{'date_object': {'$gte': '2016-04-14T18:02:02.862030', '$lte': '2016-04-28T18:02:02.862030'}}]}

Also if I try the following (using the datetime object), I get the same issue.

query = {}
query["$or"] = []
query["$or"].append({"date_object": {"$lte": time_to, "$gte": time_from } })

query = {'platform': {'$in': [re.compile('windows', re.IGNORECASE)]}, '$or': [{'date_object': {'$gte': datetime.datetime(2016, 4, 15, 7, 14, 7, 269376), '$lte': datetime.datetime(2016, 4, 29, 7, 14, 7, 269376)}}]}

The documents are like this for example.

{
        "_id": 889,
        "date_object": [
            {
                "$date": "2014-01-27T00:00:00.000Z"
            },
            {
                "$date": "2015-04-20T00:00:00.000Z"
            },
            {
                "$date": "2016-05-09T00:00:00.000Z"
            }
        ]
    }

So _id==889 is returned even though the dates are not in the range specified in the original query.

disruptive
  • 5,687
  • 15
  • 71
  • 135
  • 1
    [isoformat()](https://docs.python.org/2/library/datetime.html#datetime.date.isoformat) returns a "string", but the data is a BSON `Date`, so the you just want to pass through a regular [`datetime`](https://docs.python.org/2/library/datetime.html#datetime.datetime) instead. It's not a string, but just a serialized representation of a string in your output. – Neil Lunn Apr 14 '16 at 23:46
  • @NeilLunn I did try this initially before isoformat() but found I got the same behaviour. Any other suggestions? – disruptive Apr 15 '16 at 06:07

0 Answers0