We are storing lots of data in our Mongo DB collection. We do not customize the _id field value. I have an index on a timeStamp field in this collection. This field itself records the insertion time of the record only. Given that the ObjectID in the _id field contains a timestamp can I use MongoDB _id field default index for the same queries?

Would using the _id field be as effective as the separate timestamp index?

I am trying to improve my insertion performance as this collection takes heavy write load.

In addition to the insertion load, we run an aggregation on this collection every 5 minutes based on time range.

1 Answer 1


Yes, of course you can use _id to query created time. You just need to query "range" where lower limit is wanted time (with needed resolution, msec, sec, min...) where rest of bytes (_id) are zero and upper limit with rest of bytes set to FF.

I'm not sure is it most effective way or is saved db space worth of it...

  • what could be possible reasons for it to be non effective as time stamp index. Could you please think of some reasons for it.
    – viren
    Apr 12, 2017 at 7:30
  • With time stamp index you can search with exact value. With _id index you need to have range. Of course at the end search is so fast that you don't see any difference. You just need to make little bit more coding at your application.
    – JJussi
    Apr 12, 2017 at 7:38
  • Thanks We are using it fro range search only so it should work and yes it is reducing my index size by more than 30% so trade off can work i think.
    – viren
    Apr 12, 2017 at 8:45
  • Please vote my post.. ;-)
    – JJussi
    Apr 12, 2017 at 9:48

Your Answer

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

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