什么时候将文档添加到MongoDB集合中

编程入门 行业动态 更新时间:2024-10-14 22:23:13
本文介绍了什么时候将文档添加到MongoDB集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个现有的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集合中

本文发布于:2023-11-22 14:30:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   文档   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!