hello, @Jason_Tran . thank you for the response!
No, we use the default chunk size of 64 MB
all these are in the same chunk on the same shard as seen below
db.CompoundHashedShardKeyTest.getShardDistribution()
Shard mongos-01-shard-02 at mongos-01-shard-02/mongos-01-shard-02-01.company.net:27017,mongos-01-shard-02-02.company.net:27017
{
data: '1.1MiB',
docs: 20000,
chunks: 1,
'estimated data per chunk': '1.1MiB',
'estimated docs per chunk': 20000
}
---
Totals
{
data: '1.1MiB',
docs: 20000,
chunks: 1,
'Shard mongos-01-shard-02': [
'100 % data',
'100 % docs in cluster',
'58B avg obj size on shard'
]
}
that’s correct. After reading the blog initially, I was using the range sharding for both these fields and the distribution wasn’t good either. in fact, it was skewed towards the one shard no matter how many unique userid and date combinations of data are inserted into the collection. That’s when I inclined towards using the hash of the userid field while keeping the date field for the range sharding.
sure. That test was merely done to understand how to distribute documents belonging to one user among all the shards.
I have now inserted data corresponding to the example I mentioned. There are a total of 100 users each inserting 100 documents except for every 10th user who inserts 10 records each. So, this test setup simulates 90 users each having 100 docs while the remaining 10 users have 10 docs each.
I’m attaching the test script and results from two scenarios. first is with the shard key where there’s no hashing on either of these fields and second where there is hashing on userid field and ranged sharding on date field
My observations are
- Range sharding doesn’t seem to work as per the blog
- Hashed sharding is definitely better when we have a good amount of unique userid values but it’s not perfect (58-42)
I just want to solve this problem of distributing documents belonging to a userid containing different date values across shards to achieve better distribution and read locality when targeting documents belonging to a give input date range
Please let me know if you need more info. Thank you very much for helping! Much appreciated 
Test data script
for (var i = 1; i <= 100; i++) {
inner:
for (var j = 1; j <= 100; j++) {
var date = new Date(1640995200000 + j * 1000 * 60 * 60);
db.CompoundHashedShardKeyTest.insert({"userid":i,"created_at":date});
if (i % 10 == 0 && j >= 10) {
print("Inserted " + j + " records for user " + i);
break inner;
}
print(i + "-" + date);
}
}
Shard distribution with ranged shard key on both fields as mentioned in the blog
db.CompoundHashedShardKeyTest.getShardDistribution()
Shard mongos-01-shard-02 at mongos-01-shard-02/mongos-01-shard-02-01.company.net:27017,mongos-01-shard-02-02.company.net:27017
{
data: '479KiB',
docs: 9100,
chunks: 1,
'estimated data per chunk': '479KiB',
'estimated docs per chunk': 9100
}
---
Totals
{
data: '479KiB',
docs: 9100,
chunks: 1,
'Shard mongos-01-shard-02': [
'100 % data',
'100 % docs in cluster',
'54B avg obj size on shard'
]
}
Shard distribution with hashed-sharding on userid and range-sharding on date
db.CompoundHashedShardKeyTest.getShardDistribution()
Shard mongos-01-shard-01 at mongos-01-shard-01/mongos-01-shard-01-01.company.net:27017,mongos-01-shard-01-02.company.net:27017
{
data: '203KiB',
docs: 3860,
chunks: 2,
'estimated data per chunk': '101KiB',
'estimated docs per chunk': 1930
}
---
Shard mongos-01-shard-02 at mongos-01-shard-02/mongos-01-shard-02-01.company.net:27017,mongos-01-shard-02-02.company.net:27017
{
data: '276KiB',
docs: 5240,
chunks: 2,
'estimated data per chunk': '138KiB',
'estimated docs per chunk': 2620
}
---
Totals
{
data: '479KiB',
docs: 9100,
chunks: 4,
'Shard mongos-01-shard-01': [
'42.41 % data',
'42.41 % docs in cluster',
'54B avg obj size on shard'
],
'Shard mongos-01-shard-02': [
'57.58 % data',
'57.58 % docs in cluster',
'54B avg obj size on shard'
]
}