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??

  • ,Once you shard a collection, the shard key and the shard key values are immutable. – Md Haidar Ali Khan Dec 27 '17 at 5:46
  • @Stennie : Does empty and missing mean same thing? If empty means Field2:"" then why is it treated so differently than null? Wondering ... – Sameer Dec 29 '17 at 3:11
  • To be clear, fields in the shard key must be present in all documents and have a value (so in your case, Field1 and Field2 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. – Stennie Dec 29 '17 at 22:10
  • @Stennie: Yup I got the cardinality thing. Just that you mentioned the field cannot be empty so was little confused. Now I am sure by that you meant the field must be present and it can have any values : null/empty. Thanks for the direction. – Sameer Jan 1 '18 at 4:20

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 that collection. You cannot update the values of the shard key fields.

  • Just a copy of the documentation without any relevance to the behavior of shard keys. – Markus W Mahlberg Jan 2 '18 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.