Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-37587

Duplicate value in _id field

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 3.4.14, 3.6.8
    • Component/s: Querying, Sharding, Usability
    • None
    • ALL
    • Hide
      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 }
      
      Show
      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 }

      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. 

            Assignee:
            daniel.hatcher@mongodb.com Danny Hatcher (Inactive)
            Reporter:
            jdatta Joydip Datta
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: