Reputation: 1588
What happens, if two clients, working with one MongoDB instance, perform and insert operation at same time without «forceServerObjectId: true». Is it possible to be generated equal ObjectIDs, is there may be a conflict?
Upvotes: 2
Views: 2935
Reputation: 69663
There is an implied unique index on the _id field of every collection which makes it impossible for two objects with the same _id to exist in the same collection.
When two objects with the same _id value are stored with collection.save
, one document will replace the other.
When they are stored with collection.insert
, one of the inserts will fail with a duplicate key error.
But note that MongoDB ObjectIDs include a 24bit machine-ID. This makes it impossible for two clients to generate the same ID, unless they have the same machine-ID. And even then it's unlikely. That, of course, only applies when you let the MongoDB driver (or shell) auto-generate ObjectIDs. MongoDB allows to use any value of any type as a value for the _id field when you set it manually. When you do this (you shouldn't), it's your responsibility to ensure uniqueness.
Upvotes: 2