Mongo indexes the _id field by default, and ObjectId already starts with a timestamp, so basically Mongo already indexes your collection using input time for you. Therefore, if you use the default Mongo settings, you do not need to index the second temporary field (or even add it).
To get the rudy object id creation time:
ruby-1.9.2-p136 :001 > id = BSON::ObjectId.new => BSON::ObjectId('4d5205ed0de0696c7b000001') ruby-1.9.2-p136 :002 > id.generation_time => 2011-02-09 03:11:41 UTC
To create object identifiers within a given time:
ruby-1.9.2-p136 :003 > past_id = BSON::ObjectId.from_time(1.week.ago) => BSON::ObjectId('4d48cb970000000000000000')
So, for example, if you want to download all the documents inserted last week, you will simply look for _ids more than past_id and less id. So, through the Ruby driver:
collection.find({:_id => {:$gt => past_id, :$lt => id}}).to_a =>
You can, of course, also add a separate field for timestamps and index it, but it makes no sense to use this performance when Mongo already does the necessary work for you with its default field _id.
Additional information about object identifiers.
source share