Are the dates stored in the \'created_at\' fields marshaled to Python datetime objects via PyMongo, or do I have to manually replace the text strings with Python Date objects? i
you can parse Twitter's created_at timestamps to Python datetimes like so:
import datetime, pymongo
created_at = 'Mon Jun 8 10:51:32 +0000 2009' # Get this string from the Twitter API
dt = datetime.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
and insert them into your Mongo collection like this:
connection = pymongo.Connection('mymongohostname.com')
connection.my_database.my_collection.insert({
'created_at': dt,
# ... other info about the tweet ....
}, safe=True)
And finally, to get tweets within the last three days, newest first:
three_days_ago = datetime.datetime.utcnow() - datetime.timedelta(days=3)
tweets = list(connection.my_database.my_collection.find({
'created_at': { '$gte': three_days_ago }
}).sort([('created_at', pymongo.DESCENDING)]))