Mongodb – Is using the MongoDB _id field index as a time range query possible

indexmongodb

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.

Best Answer

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...