read timestamp from _id

929 views
Skip to first unread message

A J

unread,
Nov 30, 2011, 2:34:25 PM11/30/11
to mongodb-user
I know that the first 4 bytes of the default objectID contains the
timestamp.
1. From mongo client, given a document, how do I read the timestamp
from the object id.
2. If I want to retrieve all records inserted between two timestamps,
what would the query look like in mongo client ?

Thanks.

Tyler Brock

unread,
Nov 30, 2011, 5:58:43 PM11/30/11
to mongodb-user
1. You can use the first 4 bytes of the objectID:
http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-BSONObjectIDSpecification

2. db.posts.find({"created_on": {"$gte": start, "$lt": end}})

Where start and end are dates.

Sam Millman

unread,
Nov 30, 2011, 6:52:41 PM11/30/11
to mongod...@googlegroups.com
There also in some drivers (not sure about the js driver) functions for reading the time from the _id for you. You'll have to check which function to use in each drivers documentation but there is normally one lurking around somewhere.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Marc

unread,
Nov 30, 2011, 7:45:35 PM11/30/11
to mongodb-user
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

4) Create a new ObjectId based on this string

5) Query against this new ObjectId.

Here is an example done in the JS shell:

> db.time.drop() // ensure that the "time" collection is empty for this example
true
> db.time.save({x:1})
> db.time.save({x:2})
> db.time.save({x:3})
> query_time = new Date() //create a new date object in the middle of the inserted objects
ISODate("2011-12-01T00:27:26.316Z")
> db.time.save({x:4})
> db.time.save({x:5})
> db.time.save({x:6})
> ms = query_time.getTime()
1322699246316
> sec = ms/1000
1322699246.316
> roundsec = Math.floor(sec)
1322699246
> hex = roundsec.toString(16)
4ed6c9ee
> id_string = hex + "0000000000000000"
4ed6c9ee0000000000000000
> my_id = ObjectId(id_string)
ObjectId("4ed6c9ee0000000000000000")
> db.time.find({_id:{$lt:my_id}}) //find the documents created before query_time
{ "_id" : ObjectId("4ed6c9ce2edc10a978c6ce9e"), "x" : 1 }
{ "_id" : ObjectId("4ed6c9d42edc10a978c6ce9f"), "x" : 2 }
{ "_id" : ObjectId("4ed6c9d92edc10a978c6cea0"), "x" : 3 }
> db.time.find({_id:{$gt:my_id}}) //find the documents created after query_time
{ "_id" : ObjectId("4ed6c9f32edc10a978c6cea1"), "x" : 4 }
{ "_id" : ObjectId("4ed6c9f62edc10a978c6cea2"), "x" : 5 }
{ "_id" : ObjectId("4ed6c9f92edc10a978c6cea3"), "x" : 6 }
>

Hopefully, the above will help you to accomplish what you need to do.
Good luck!

A J

unread,
Dec 1, 2011, 12:11:50 PM12/1/11
to mongodb-user
This is great info Marc ! Thanks.

On Nov 30, 7:45 pm, Marc <m...@10gen.com> 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....

Reply all
Reply to author
Forward
0 new messages