简体   繁体   中英

MongoDb sharding and Unique Constraints

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM