[DOCS-11095] Clarify that fields in a hashed shard key are required and immutable Created: 07/Dec/17  Updated: 30/Oct/23

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Improvement Priority: Minor - P4
Reporter: Kelsey Schubert Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 1 year, 14 weeks, 2 days ago
Epic Link: DOCSP-1769

 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"
	}
})



 Comments   
Comment by Education Bot [ 31/Oct/22 ]

Hello! This ticket has been closed due to inactivity. If you believe this ticket is still important, please reopen it and leave a comment to explain why. Thank you!

Comment by Kelsey Schubert [ 07/Dec/17 ]

Probably a similar note as on https://docs.mongodb.com/manual/core/sharding-shard-key/ would suffice, but it may be worth calling out on both pages that the shard key field must be explicitly set.

Generated at Thu Feb 08 08:02:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.