how to get creation date from object id in mongoose?
I believe ObjectId has a getTimestamp() method; e.g.
_id.getTimestamp()
If you run this you'll get the timeStamp
console.log(new mongoose.Types.ObjectId().getTimestamp() );
eg.
Fri Nov 16 2012 17:20:14 GMT+0000 (GMT)
You can create a virtual 'created' property on the mongoose schema that uses the _id to get the creation timestamp. Just add:
YourMongooseSchema.virtual('created').get( function () {
if (this["_created"]) return this["_created"];
return this["_created"] = this._id.getTimestamp();
});
var timestamp = document._id.toString().substring(0,8);
var date = new Date( parseInt( timestamp, 16 ) * 1000 );