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

Passing a complete document to update can break shard key immutability.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.8.2, 2.0.0
    • Component/s: None
    • None
    • ALL

      Shard keys are supposed to be immutable. This is enforced during update when using an update modifier like $set but not when passing an entire document:

      mongos> db.runCommand({shardcollection: 'foo.things', key:

      {'foobar': 1}

      , unique: true})

      { "collectionsharded" : "foo.things", "ok" : 1 }

      ...
      mongos> db.things.find()

      { "_id" : ObjectId("4e7d176c3c3472e653777dce"), "foobar" : 123 }

      mongos> db.things.update(

      {'foobar': 123}

      , {'$set': {'foobar': 456}})
      Can't modify shard key's value fieldfoobar for collection: foo.things
      mongos> doc = db.things.findOne()

      { "_id" : ObjectId("4e7d176c3c3472e653777dce"), "foobar" : 123 }

      mongos> doc.foobar = 456
      456
      mongos> db.things.save(doc)
      mongos> db.things.find()

      { "_id" : ObjectId("4e7d176c3c3472e653777dce"), "foobar" : 456 }

      See the attached js for an example using update directly (without upsert).

        1. mutable.js
          0.7 kB
          Bernie Hackett

            Assignee:
            Unassigned Unassigned
            Reporter:
            bernie@mongodb.com Bernie Hackett
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: