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

前端 未结 2 1889
旧时难觅i
旧时难觅i 2021-02-06 06:06

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

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 06:51

    1) Test it out and see :-)

    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. The point of pymongo really is to be working in native python objects. You don't really set dates in the BSON format with $date. 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.

    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

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

    How do I convert a property in MongoDB from text to date type?

提交回复
热议问题