[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:

mongos> use bad
switched to db bad
mongos> sh.shardCollection("bad.coll1", { "age": "hashed" })
{ "code" : 26, "ok" : 0, "errmsg" : "database bad not found" }
mongos> sh.enableSharding("bad")
{ "ok" : 1 }
mongos> sh.shardCollection("bad.coll1", { "age": "hashed" })
{ "collectionsharded" : "bad.coll1", "ok" : 1 }
mongos> db.coll1.insert({_id : 1, "name" : "a", "age" : 10})
WriteResult({ "nInserted" : 1 })
mongos> db.coll1.update({_id: 1}, {_id : 1, "name" : "a", "age" : 11}, {upsert: true})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
mongos> db.coll1.find()
{ "_id" : 1, "name" : "a", "age" : 11 }
{ "_id" : 1, "name" : "a", "age" : 10 }
mongos> db.coll1.update({_id: 1}, {_id : 1, "name" : "a", "age" : 11}, {upsert: true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
mongos> db.coll1.update({_id: 1}, {_id : 1, "name" : "a", "age" : 12}, {upsert: true})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
mongos> db.coll1.update({_id: 1}, {_id : 1, "name" : "a", "age" : 13}, {upsert: true})
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 66,
                "errmsg" : "After applying the update to the document {age: 10.0 , ...}, the (immutable) field 'age' was found to have been altered to age: 13.0"
        }
})
mongos> 
 
mongos> version()
3.4.14 
 
mongos> db.coll1.find()
{ "_id" : 1, "name" : "a", "age" : 12 }
{ "_id" : 1, "name" : "a", "age" : 11 }
{ "_id" : 1, "name" : "a", "age" : 10 }

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

mongos> db.coll1.update({_id: 1}, {_id : 1, "name" : "a", "age" : 13}, {upsert: true})
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 66,
                "errmsg" : "After applying the update to the document {age: 10.0 , ...}, the (immutable) field 'age' was found to have been altered to age: 13.0"
        }
})
mongos> 

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

Generated at Thu Feb 08 04:46:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.