ObjectId to Timestamp
Extract timestamp info from MongoDB ObjectIdObjectId
(NOTE: not unique, only use for comparisons, not for creating new documents!)
(convenient to paste into mongo shell)
Why generate an ObjectId from a timestamp?
To query documents by creation date.
e.g. to find all comments created after 2020-08-05 00:00:00 UTC+7:
db.comments.find({_id: {$gt: ObjectId("5f2994100000000000000000")}})
Javascript functions
var objectIdFromDate = function (date) { return Math.floor(date.getTime() / 1000).toString(16) + "0000000000000000"; }; var dateFromObjectId = function (objectId) { return new Date(parseInt(objectId.substring(0, 8), 16) * 1000); };
Forked from SteveRidout/mongo-object-time