我有一个现有的mongodb集合,该集合没有有关文档创建时间的任何信息.
I have an existing mongodb collection, which doesn't have any information about when the document was created.
是否有可能以某种方式获取此信息?我浏览了所有文档,但在任何地方都看不到.
Is it possible to get this information some how? I've had a look through the docs but can't see it anywhere.
推荐答案如果您为_id属性使用默认的ObjectId值,则创建时间将在其中进行编码.
If you are using the default ObjectId value for your _id attribute, the creation time is encoded inside it.
如 ObjectID文档所述:
ObjectId是一种12字节的BSON类型,使用以下方式构造:
ObjectId is a 12-byte BSON type, constructed using:
- 一个4字节的值,表示自Unix时代以来的秒,
- 3字节机器标识符
- 从随机值开始的2字节进程ID和3字节计数器.
- a 4-byte value representing the seconds since the Unix epoch,
- a 3-byte machine identifier
- a 2-byte process id, and a 3-byte counter, starting with a random value.
您可以在ObjectId对象上调用getTimestamp()函数,以获取包含对象创建时间的ISODate对象:
You can call the getTimestamp() function on an ObjectId object to get an ISODate object containing the creation time of the object:
在mongo shell中:
ObjectId().getTimestamp() ISODate("2014-05-14T14:29:12Z")更多推荐
什么时候将文档添加到MongoDB集合中
发布评论