Marc | 1 Dec 2011 01:45

Re: read timestamp from _id

Extracting the time stamp from Mongo ObjectIds is explained in the
Mongo Document "Optimizing Object IDs"
http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Extractinsertiontimesfromidratherthanhavingaseparatetimestampfield.

There is more information about the Mongo ObjectID Datatype here:
http://www.mongodb.org/display/DOCS/Object+IDs

Here is how it is done in the Mongo JS Shell:

> my_id = db.test_collection.findOne()._id
ObjectId("4ed677329217cbcb7801c448")
> my_id.getTimestamp()
ISODate("2011-11-30T18:34:26Z")

Just about all of the language drivers implement methods for
extracting these timestamps.  Please refer to the relevant api docs
for details.

Querying based on the timestamps embedded in ObjectIds is possible
with a little bit of legwork.  I have been informed that this is "not
recommended", and if possible, a new field containing the creation
date should be added.  However, here is how it is done:

1) Find the number of seconds from the Epoch (seconds, NOT
milliseconds) of your desired query date.

2) Convert this number to a hexidecimal string

3) The hexadecimal string should be 8 characters long.  Add 16 zeros
to it, to bring the length to 24 characters
(Continue reading)

A J | 1 Dec 2011 18:11
Picon

Re: read timestamp from _id

This is great info Marc ! Thanks.

On Nov 30, 7:45 pm, Marc <m...@...> wrote:
> Extracting the time stamp from Mongo ObjectIds is explained in the
> Mongo Document "Optimizing Object IDs"http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingO....
>
> There is more information about the Mongo ObjectID Datatype here:http://www.mongodb.org/display/DOCS/Object+IDs
>
> Here is how it is done in the Mongo JS Shell:
>
> > my_id = db.test_collection.findOne()._id
>
> ObjectId("4ed677329217cbcb7801c448")> my_id.getTimestamp()
>
> ISODate("2011-11-30T18:34:26Z")
>
> Just about all of the language drivers implement methods for
> extracting these timestamps.  Please refer to the relevant api docs
> for details.
>
> Querying based on the timestamps embedded in ObjectIds is possible
> with a little bit of legwork.  I have been informed that this is "not
> recommended", and if possible, a new field containing the creation
> date should be added.  However, here is how it is done:
>
> 1) Find the number of seconds from the Epoch (seconds, NOT
> milliseconds) of your desired query date.
>
> 2) Convert this number to a hexidecimal string
>
(Continue reading)


Gmane