Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11095

Clarify that fields in a hashed shard key are required and immutable

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Won't Do
    • Icon: Minor - P4 Minor - P4
    • Server_Docs_20231030
    • None
    • manual
    • None

    Description

      https://docs.mongodb.com/manual/core/hashed-sharding/

      I think it may be a good idea to clarify that hashed shard key field is required an immutable in the docs. Here is some sample output a user might encounter if they didn't realize this restriction.

      output generated on 3.4.6:

      mongos> sh.shardCollection("test.foo",{x:"hashed"})
      { "collectionsharded" : "test.foo", "ok" : 1 }
      mongos> db.foo.insert({})
      WriteResult({
      	"nInserted" : 0,
      	"writeError" : {
      		"code" : 61,
      		"errmsg" : "document { _id: ObjectId('5a29a6dd17fb6b18f562c0b8') } does not contain shard key for pattern { x: \"hashed\" }"
      	}
      })
      mongos> db.foo.insert({x:1})
      WriteResult({ "nInserted" : 1 })
      mongos> db.foo.update({x:1},{$unset: {x:1}})
      WriteResult({
      	"nMatched" : 0,
      	"nUpserted" : 0,
      	"nModified" : 0,
      	"writeError" : {
      		"code" : 66,
      		"errmsg" : "After applying the update to the document with _id: ObjectId('5a29a6fb17fb6b18f562c0b9'), the 'x' (required and immutable) field was found to have been removed --{ _id: ObjectId('5a29a6fb17fb6b18f562c0b9'), x: 1.0 }"
      	}
      })
      mongos> db.foo.find()
      { "_id" : ObjectId("5a29a6fb17fb6b18f562c0b9"), "x" : 1 }
      mongos> db.foo.update({x:1},{x:2})
      WriteResult({
      	"nMatched" : 0,
      	"nUpserted" : 0,
      	"nModified" : 0,
      	"writeError" : {
      		"code" : 66,
      		"errmsg" : "After applying the update to the document {x: 1.0 , ...}, the (immutable) field 'x' was found to have been altered to x: 2.0"
      	}
      })
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            kelsey.schubert@mongodb.com Kelsey Schubert
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              1 year, 14 weeks, 2 days ago