I want to change it to a unix timestamp object to ensure uniqueness.
Unix timestamps are not as unique as you think they are as stated by wikipedia (implementation sometimes differ): http://en.wikipedia.org/wiki/Unix_time
Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC),
It is because of the granularity of the UNIX timestamp that the ObjectId has an additional inc compound to it, if an operation occurs the same second that inc is increased. It is actually quite common for that inc to be increased in a large database.
If you use timestamp alone you WILL face problems.
Instead I would recommend you either:
- House two IDs, one user friendly and one not
- look for something else to replace it, I do not know enough of your scenario to tell you what
Is there a way to customize how mongo generates the objectId for a specific collection?
As the answer by @the_man_slim shows in python, you can insert your own id however, you cannot update the _id
field so beware of that.