I have a shard key based on {Field1:1,Field2:1} where Field2 can be null. So if two documents have same value for Field1 and null/empty value of Field2 then are they considered to belong to the same shard key/non split-able??
2 Answers
In MongoDB 'null' is just another value so two documents that have the same values for field1 (some non-null) and same value for field2 (null) have the same value for the shard key and will always be in the same "chunk".
As per MongoDB BOL
Here The shard key determines the distribution of the collection’s documents among the cluster’s shards. The shard key is either an indexed field or indexed compound fields that exists in every document in the collection.
MongoDB partitions data in the collection using ranges of shard key values. Each range defines a non-overlapping range of shard key values and is associated with a chunk.
MongoDB attempts to distribute chunks evenly among the shards in the cluster. The shard key has a direct relationship to the effectiveness of chunk distribution.
Note: Once you shard a collection, the shard key and the shard key values are immutable; i.e. You cannot select a
different shard key
for thatcollection
. You cannotupdate
thevalues
of theshard key fields
.
-
Just a copy of the documentation without any relevance to the behavior of shard keys. Commented Jan 2, 2018 at 11:19
Field1
andField2
must always be set). Values include null or empty string, although if those are common in one of your shard key fields you should consider a field with more cardinality.