When I used a hashed shard key all of my shards are already split with chunks before adding any data as in the example output from sh.status() below.
sample.test
shard key: { "sampleId" : "hashed" }
unique: false
balancing: true
chunks:
shard001 2
shard002 2
{ "sampleId" : { "$minKey" : 1 } } -->> { "sampleId" : NumberLong("-4611686018427387902") } on : shard001 Timestamp(1, 0)
{ "sampleId" : NumberLong("-4611686018427387902") } -->> { "sampleId" : NumberLong(0) } on : shard001 Timestamp(1, 1)
{ "sampleId" : NumberLong(0) } -->> { "sampleId" : NumberLong("4611686018427387902") } on : shard002 Timestamp(1, 2)
{ "sampleId" : NumberLong("4611686018427387902") } -->> { "sampleId" : { "$maxKey" : 1 } } on : shard002 Timestamp(1, 3)
Now when using a compound key or an unhashed key and running sh.status() I see only one of my shards show up containing 1 chunk. Now I have to fill it to the 64mb max in order for a second chunk to be created. In the example below I have a shard002 and a shard001.
sample.test
shard key: { "sampleId" : 1, "uid" : 1 }
unique: false
balancing: true
chunks:
shard002 1
{ "sampleId" : { "$minKey" : 1 }, "uid" : { "$minKey" : 1 } } -->> { "sampleId" : { "$maxKey" : 1 }, "uid" : { "$maxKey" : 1 } } on : shard002 Timestamp(1, 0)
So my question is why are the chunks generated when there is no data when using a hashed shard key?
This is the actual behaviour of Hashed Sharding.
As per the mongodb documentation
If you shard an empty collection using a hashed shard key:
With no zones and zone ranges specified for the empty or non-existing collection: The sharding operation creates empty chunks to cover the entire range of the shard key values and performs an initial chunk distribution. By default, the operation creates 2 chunks per shard and migrates across the cluster. You can use numInitialChunks option to specify a different number of initial chunks. This initial creation and distribution of chunks allows for faster setup of sharding. After the initial distribution, the balancer manages the chunk distribution going forward.