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.
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.
If you are using the default ObjectId
value for your _id
attribute, the creation time is encoded inside it.
As stated in the ObjectID documentation:
ObjectId is a 12-byte BSON type, constructed using:
- 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.
You can call the getTimestamp()
function on an ObjectId
object to get an ISODate
object containing the creation time of the object:
In the mongo shell:
ObjectId().getTimestamp()
ISODate("2014-05-14T14:29:12Z")
There is most likely the actual _id
value of the document unless you have replaced this with something else.
In the simple "JavaScript" syntax of this ( and various methods are available to other languages) you simply access this as:
var id = new ObjectId();
id.getTimetstamp();
Various language implementations have a way of retrieving the "timestamp" from an ObjectId
value so you can just use that.