Python Mongo Timestamp based Date Range query

918 views
Skip to first unread message

creatiwit

unread,
May 30, 2012, 12:55:06 AM5/30/12
to mongod...@googlegroups.com
This is driving me a bit batty. I load the data into Mongo using the C++ driver and I epochs in my timestamp field. Mongo stores it as UTC.

Now, when I go to query it with pymongo, I pass in a start and end time using datetime objects and do a simple time range query. Now, this is the tricky part, do I need to convert my Datetime objects based on UTC,  or does the Mongo driver take care of that if it sees no UTC specified in the Datetime object. 

What is the right way to do time range queries in Mongo and python, epochs are just much easier to deal with than this.

Nat

unread,
May 30, 2012, 1:44:30 AM5/30/12
to mongod...@googlegroups.com

Ross Lawley

unread,
May 30, 2012, 5:22:36 AM5/30/12
to mongod...@googlegroups.com

You can set your connection to be timezone aware - for saving data, but when querying you should use datetime objects based on UTC.

Ross

creatiwit

unread,
May 30, 2012, 12:38:42 PM5/30/12
to mongod...@googlegroups.com
Got it it was not clear from the documentation what tz_aware was actually converting. But after playing around for a bit with the datetime and extracting Timestamps, its makes sense now.
Reply all
Reply to author
Forward
0 new messages