3

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?

1 Answer 1

10

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")
    }
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.