Mongo _id as string indexed key. Good or bad?

key, mongodb

Solution

If there is a more natural primary key to use than an ObjectID (for example, a string value) feel free to use it.

The purpose of ObjectIDs is to allow distributed clients to quickly and independently generate unique identifiers according to a standard formula. The 12-byte ObjectID formula includes a 4-byte timestamp, 3-byte machine identifier, 2 byte process ID, and a 3-byte counter starting with a random value.

If you generate/assign your own unique identifiers (i.e. using strings), the potential performance consideration is that you won't if know this name is unique until you try to insert your document using that `_id`. In this case you would need to handle the duplicate key exception by retrying the insert with a new `_id`.

Problem

I'm developing an API that the only method to get a resource is providing a string key like my_resource. It's a good practice to override _id (this make some mongodb drivers more easy to use) or its bad? What about in the long term? Thank you

Original source