How to compare dates with Twitter data stored in MongoDB via PyMongo?

Are dates stored in 'created_at' fields marshaled by Python datetime objects via PyMongo, or do I need to manually replace text strings with Python Date objects? those.

How to convert property in MongoDB from text to date?

It seems very unnatural that I would have to replace date strings with Python date objects, so I ask a question.

I would like to write queries displaying tweets in the last three days. Please let me know if there is a way to do this. Thanks!

+7
source share
2 answers

you can cast Twitter created_at timestamps for Python dates like this:

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 paste them into your Mongo collection as follows:

 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 in the last three days, first the newest:

 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)])) 
+16
source

1) Check it out and see :-)

2) If you use pymongo and you set the field to a python datetime object, then it will store it in the correct bson format and return datetime objects back to you when you request it. The pymongo point should really work in native python objects. You do not set BSON dates with $ date. He does it under the hood. Having said that, I do not know how pymongo would know to set you a lowercase date without being able to tell its date.

3) If the values ​​are already in mongodb as raw strings, then you will need to convert them yourself on the client side using string formatting: http://docs.python.org/library/datetime.html#strftime-strptime-behavior

Edit: if you want to run a function on mongodb that converts your string dates to date:

How to convert property in MongoDB from text to date?

0
source

All Articles