简体   繁体   English

如何通过PyMongo比较存储在MongoDB中的Twitter数据的日期?

[英]How do I compare dates from Twitter data stored in MongoDB via PyMongo?

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? 存储在'created_at'字段中的日期是否通过PyMongo封送到Python日期时间对象,还是我必须用Python Date对象手动替换文本字符串? ie

How do I convert a property in MongoDB from text to date type? 如何将MongoDB中的属性从文本转换为日期类型?

It seems highly unnatural that I would have to replace the date strings with Python date objects, which is why I'm asking the question. 我不得不用Python日期对象替换日期字符串似乎非常不自然,这就是为什么我在问这个问题。

I would like to write queries that display the tweets from the past three days. 我想编写显示过去三天推文的查询。 Please let me know if there is a slick way of doing this. 如果有一个光滑的方式,请告诉我。 Thanks! 谢谢!

you can parse Twitter's created_at timestamps to Python datetimes like so: 您可以将Twitter的created_at时间戳解析为Python日期时间,如下所示:

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: 并将它们插入您的Mongo集合中,如下所示:

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)]))

1) Test it out and see :-) 1)测试一下,看看:-)

2) If you are using pymongo, and you set a field to be equal to a python datetime object, then it will store it in the proper bson format and return datetime objects back to you when you query it. 2)如果您正在使用pymongo,并且您将字段设置为等于python datetime对象,那么它将以正确的bson格式存储它,并在您查询时将datetime对象返回给您。 The point of pymongo really is to be working in native python objects. pymongo的观点实际上是在本机python对象中工作。 You don't really set dates in the BSON format with $date. 您没有使用$ date真正设置BSON格式的日期。 It does that under the hood. 它在引擎盖下做到了。 That being said, I'm not sure how pymongo would know to marshal a string date for you, without the ability to say its a date. 话虽这么说,我不知道pymongo会怎么知道为你编组一个字符串日期,而没有能力说出约会。

3) If you values are already in mongodb as raw strings, then you would need to convert them yourself on the client side using string formatting: http://docs.python.org/library/datetime.html#strftime-strptime-behavior 3)如果你的值已经在mongodb中作为原始字符串,那么你需要使用字符串格式在客户端自己转换它们: 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 objects: 编辑:如果要在mongodb上运行将字符串日期转换为日期对象的函数:

How do I convert a property in MongoDB from text to date type? 如何将MongoDB中的属性从文本转换为日期类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM