Dealing with timezone dates in MongoDB and pymongo
<p>I do not seem to be able to query records and get what I expect. For example I am searching </p>

<pre><code>today = datetime.datetime.today()
past = today + timedelta(days=-200)
results = mongo.stuff.find({"date_added": {"gt": past}}, {"id":1})
</code></pre>

<p>I have the following date specified in MongoDB:</p>

<pre><code>"date_added": {
        "$date": "2016-04-19T18:47:54.101Z"
    },
</code></pre>

<p>But I get no results! Is this down to the timezone which appears in the MongoDB date which is screwing things up.</p>

<p>Use an aware datetime object (with timezone info). </p>

<pre><code># E.g. with UTC timezone : 
import pytz
import datetime

today = datetime.datetime.today()
past = today + timedelta(days=-200)

pytc.utc.localize(past)

results = mongo.stuff.find({"date_added": {"gt": past}}, {"id":1})
</code></pre>

<p>To use a different timezone to localize, try something like <code>pytz.timezone('US/Mountain')</code> </p>

<p>P.S. you'll need <code>pip install pytz</code></p>

python mongodb pymongo