mongodb 如何存储时间戳?创建和更新的字段是否自动可用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3727282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store timestamps? Are created and updated fields available automatically?
提问by ma11hew28
What's the best way to store timestamps in MongoDB?
在 MongoDB 中存储时间戳的最佳方法是什么?
Which format is best:
# "created": { "d" : "2010-03-29", "t" : "20:15:34" } # "created": "12343545234" # seconds since epoc # "created": "2010-03-14T21:20:14+0000"
Is there a way to have MongoDB automatically set
created
andupdated
fields?
哪种格式最好:
# "created": { "d" : "2010-03-29", "t" : "20:15:34" } # "created": "12343545234" # seconds since epoc # "created": "2010-03-14T21:20:14+0000"
有没有办法让 MongoDB 自动设置
created
和updated
字段?
采纳答案by Jonas Elfstr?m
Which format is best
哪种格式最好
Best for what?
最适合什么?
Is there a way to have MongoDB automatically set created and updated fields?
有没有办法让 MongoDB 自动设置创建和更新的字段?
Created time is in the ObjectIdbut, as far as I know, you will have to update a updated
field manually.
创建时间在ObjectId 中,但据我所知,您必须updated
手动更新字段。
Example:
例子:
ObjectId("538141a9615760fd04ffef5f").getTimestamp()
回答by halfdan
The format you need to process it with best performance in your application should be preferred. Note that as default every document in MongoDB gets a created timestamp (http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-DocumentTimestamps)
See 1) + I think you need to manually set the "update" field.
应首选在应用程序中以最佳性能处理它所需的格式。请注意,默认情况下 MongoDB 中的每个文档都会获得一个创建的时间戳(http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-DocumentTimestamps)
请参阅 1) + 我认为您需要手动设置“更新”字段。
回答by Yogesh
If you do following on mongo shell it shows you time stamp that represents when that documents inserted using mongoId. For ex. ObjectId("51f3dee5ee49f9b91e0db133").getTimestamp()
, then it returns ISODate
.
如果您在 mongo shell 上执行以下操作,它会向您显示时间戳,该时间戳表示使用 mongoId 插入该文档的时间。例如。ObjectId("51f3dee5ee49f9b91e0db133").getTimestamp()
,然后返回ISODate
。
回答by Leopd
In 1.8 and above Mongo has a way to create 64-bit timestamp values which are guaranteed unique on a single server. But they're not updated automatically. http://www.mongodb.org/display/DOCS/Timestamp+data+type
在 1.8 及更高版本中,Mongo 有一种方法可以创建 64 位时间戳值,这些值保证在单个服务器上是唯一的。但它们不会自动更新。 http://www.mongodb.org/display/DOCS/Timestamp+data+type