What is the best workaround to achieve uniqueness and sharding? for instance we want to shard a user
collection based on unique username
that users choose for themselves and it is immutable
I think the best solution is hashed shading but unfortunately we cannot enforce uniqueness for hashed index
So is ranged sharding good? is there recommended solution?
If you really need complete uniqueness for this field (user name) - you could just have it as a "normal" (Single Field - ascending/descending) sharding key.
In that case, each document will be "individual", as it has no bulk requirements at all.
However, it will make your Index to be larger, while usually hashed sharding would do (yes, you cannot ensure uniqueness, but unless you have a very special case - it should not be a real problem)
You may want to take a lok at this discussion: Mongodb performance difference between Hash and Ascending indices (Any reason not to use hash in a not ordered field?)