Bernardo Amorim
Bernardo Amorim

Reputation: 343

Mongo _id as string indexed key. Good or bad?

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

Upvotes: 5

Views: 2645

Answers (2)

Stennie
Stennie

Reputation: 65443

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.

Upvotes: 4

LoganEtherton
LoganEtherton

Reputation: 495

In my experience, overriding _id is not the best idea. Only if your data has a value field that is naturally unique and can easily be used to replace _id should _id be overridden. But it wouldn't make a whole lot of sense to override _id only to replace it with a contrived value.

I would recommend against it for a few reasons:

First of all, doing so requires an additional implementation to handle the inevitable instances when your "unique" values will conflict. And this will almost certainly arise in a database of any significant size. This can be a problem, since MongoDB can be unforgiving when it comes to overwriting values and generally handling conflicts. In other words, you're almost certain to overwrite values or meet unhandled exceptions unless you design your database structure very carefully from the beginning.

Second, and equally important: ObjectIDs naturally have an optimized insertion formula which allows for a very good creation of indexes. When a new document is inserted, that ObjectID is created to be mathematically as close as possible to the previous ObjectID, optimizing memory and indexing capabilities. It might be more trouble than it's worth to recreate this very handy item yourself.

And lastly, although this isn't as significant, overriding _id makes it that much harder to use the standard ObjectID methods.

Now, there is at least one positive that I can think of for overriding the ObjectID:

If there is an instance when _id will certainly never be used in your database, then it can save you a good amount of memory, as indexes are pretty costly in MongoDB.

Upvotes: 2

Related Questions