How do I extract the created date out of a Mongo ObjectID

前端 未结 4 544
心在旅途
心在旅途 2020-12-04 08:23

I\'m using the Mongo shell to query my Mongo db. I want to use the timestamp contained in the ObjectID as part of my query and also as a column to extract into output. I h

4条回答
  •  自闭症患者
    2020-12-04 08:46

    In python you can do this:

    >>> from bson.objectid import ObjectId
    >>> gen_time = datetime.datetime(2010, 1, 1)
    >>> dummy_id = ObjectId.from_datetime(gen_time)
    >>> result = collection.find({"_id": {"$lt": dummy_id}})
    

    I think, ObjectId.from_datetime() - its a useful method of standard bson lib Maybe other language bindings have alternative builtin function. Source: http://api.mongodb.org/python/current/api/bson/objectid.html

提交回复
热议问题