Let's say I have a collection called Articles
. If I were to insert a new document into that collection without providing a value for the _id
field, MongoDB will generate one for me that is specific to the machine and the time of the operation (e.g. sdf4sd89fds78hj
).
However, I do have the ability to pass a value for MongoDB to use as the value of the _id
key (e.g. 1
).
My question is, are there any advantages to using my own custom _id
s, or is it best to just let Mongo do its thing? In what scenarios would I need to assign a custom _id
?
Update
For anyone else that may find this. The general idea (as I understand it) is that there's nothing wrong with assigning your own _id
s, but it forces you to maintain unique values within your application layer, which is a PITA, and requires an extra query before every insert
to make sure you don't accidentally duplicate a value.
Sammaye provides an excellent answer here: Is it bad to change _id type in MongoDB to integer?