[SERVER-37587] Duplicate value in _id field Created: 12/Oct/18 Updated: 27/Oct/23 Resolved: 19/Oct/18 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Querying, Sharding, Usability |
| Affects Version/s: | 3.4.14, 3.6.8 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Critical - P2 |
| Reporter: | Joydip Datta | Assignee: | Danny Hatcher (Inactive) |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL | ||||||||||||||||||||||||||||||||||||||
| Steps To Reproduce: |
|
||||||||||||||||||||||||||||||||||||||
| Participants: |
| Description |
|
As per documentation found here: https://docs.mongodb.com/manual/core/document/ The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
But we found a scenario as defined in the "Steps to reproduce" below where we could add two documents with the same "_id" value.
Does the uniqueness restriction on _id field always hold true?
We could reproduce it in MongoDB version 3.6 as well. |
| Comments |
| Comment by Danny Hatcher (Inactive) [ 19/Oct/18 ] | |||||||||||
|
Unique indexes are not supported across shards except as the shard key. | |||||||||||
| Comment by Danny Hatcher (Inactive) [ 19/Oct/18 ] | |||||||||||
|
Because age is the shard key in this example, it is an immutable field. Since it is immutable, it cannot be changed. Thus, when your upsert finds a match for _id:1 and it tries to update the document, it fails because the age field cannot be changed. If you expect a field to ever be updated, you should not make it your shard key. | |||||||||||
| Comment by Joydip Datta [ 16/Oct/18 ] | |||||||||||
|
Yes; I am curious about the reason for the subsequent failure
| |||||||||||
| Comment by Danny Hatcher (Inactive) [ 15/Oct/18 ] | |||||||||||
|
Hello Joydip, Because of the way that sharding is implemented, unique indexes are not supported across shards except for the shard key. Thus, if you are not sharding based on _id, the unique constraints on that field will not be enforced. Do you have any further questions? Danny Hatcher |