1

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

4
  • ,Once you shard a collection, the shard key and the shard key values are immutable. Commented Dec 27, 2017 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
    Commented Dec 29, 2017 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
    Commented Dec 29, 2017 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
    Commented Jan 1, 2018 at 4:20

2 Answers 2

2

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".

-1

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.

1
  • Just a copy of the documentation without any relevance to the behavior of shard keys. Commented Jan 2, 2018 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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