uses for mongodb ObjectId creation time

前端 未结 6 1781
逝去的感伤
逝去的感伤 2020-12-04 11:04

The ObjectId used as the default key in mongodb documents has embedded timestamp (calling objectid.generation_time returns a datetime object). So it is possible

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 11:22

    Yes, you can use the generation_time of BSON ObjectId for the purposes you want. So,

    db.collection.find().sort({ _id : -1 }).limit(10)
    

    will return the last 10 created items. However, since the embedded timestamps have a one second precision, multiple items within any second are stored in the order of their creation.

提交回复
热议问题