Projection to extract date from ObjectID
mongodb
Solution
Take a look at `ObjectId.getTimestamp()` (Documentation).
You can also `map` (Documentation) this function with your query.
db.users.find({}, {name: 1, _id : 1}).map(function(u) { return {name: u.name, created: u._id.getTimestamp() } });
Returns a list with custom `user` objects having the `name` and new `created` property.
[
{
"name" : "Jack",
"created" : ISODate("2014-01-03T21:04:19Z")
},
{
"name" : John,
"created" : ISODate("2014-01-07T18:12:50Z")
}
]
Problem
I'd like to do something like this: ``` db.users.find({}, {name: 1, 'timestampFrom(_id)': 1}) ``` So that I can see creation timestamps against usernames Is there a way of extracting the timestamp from the ObjectID in a projection?