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

Duplicate value in _id field

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Works as Designed
    • Affects Version/s: 3.4.14, 3.6.8
    • Fix Version/s: None
    • Component/s: Querying, Sharding, Usability
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      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 }

      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. 

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              12 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: