Reputation: 31
I am trying to create shard key from an array but it is showing me error -
errmsg" : "can't shard collection 'inno.inno_pub' with unique index on { mid: 1.0 } and proposed shard key { mid: 1.0, t.category: 1.0, t.value: 1.0 }. "Uniqueness can't be maintained unless shard key is a prefix"
Upvotes: 2
Views: 1802
Reputation: 21639
As per release 4.4
Refinable shard keys are introduced, which simply means you can keep rebalancing with a more fine-grained level even after you've defined the shard ley by adding/suffix/appending the key for better distribution/cardinality as per the data growth/added new shards in cluster
Copied from the official source:
MongoDB 4.4 brings the ability to refine the shard key itself, and to create compound hashed shard keys to allow for an even more fine-grained distribution of data within the cluster.
Shard keys cannot be arrays.
sh.shardCollection()
will fail if any key has an array value and inserting an array into that field is not allowed.
Holds good with version 4.2
But, as an important note with the release of new version starting 4.2, the following statement does not apply.
"Once inserted, a document's shard key value cannot be modified" .
So the answer to the question, Can shard key be changed?
Although you cannot select a different shard key for a sharded collection, starting in MongoDB 4.2, you can update a document's shard key value unless the shard key field is the immutable _id field
Upvotes: 0
Reputation: 103445
From Kristina Chodorow's book MongoDB - The Definitive Guide:
Shard keys cannot be arrays.
sh.shardCollection()
will fail if any key has an array value and inserting an array into that field is not allowed. Once inserted, a document's shard key value cannot be modified. To change a document's shard key, you must remove the document, change the key, and reinsert it. Thus, you should choose a field that is unchangeable or changes frequently.
Upvotes: 2