简体   繁体   中英

mongodb range sharding with string field

I use 'id' field in mongodb documents which is the HASH of '_id' (ObjectId field generated by mongo). I want to use RANGE sharding with 'id' field. The question is the following:

How can I set ranges for each shard when 'shardKey' is some long String (for example 64 chars)?

If you want your data to be distributed based on a hash key, MongoDB has a built-in way of doing that:

sh.shardCollection("yourDB.yourCollection", { _id: "hashed" })

This way, data will be distributed between your shards randomly, as well as uniformly (or very close to it) .

Please note that you can't have both logical key ranges and random data distribution. It's either one or the other, they are mutually exclusive. So:

  • If you want random data distribution, use { fieldName: "hashed" } as your shard key definition.
  • If you want to manually control how data is distributed and accessed, use a normal shard key and define shard tags .

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