What’s the best way to store timestamps in MongoDB?
Which format is best:
1
2
3
# “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 setcreated and updated fields in MongoDB?
Which format is best
Best for what in MongoDB?
Is there a way to have MongoDB automatically set created and updated fields in MongoDB?
Created time is in the ObjectId but, as far as I know, you will have to update a updated field manually.
Example:
1
ObjectId(“538141a9615760fd04ffef5f”).getTimestamp()
1. 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
(https://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-DocumentTimestamps)
2. See 1) + I think you need to manually set the “update” field.
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.