简体   繁体   English

带字符串字段的mongodb范围分片

[英]mongodb range sharding with string field

I use 'id' field in mongodb documents which is the HASH of '_id' (ObjectId field generated by mongo). 我在mongodb文档中使用“ id”字段,这是“ _id”(由mongo生成的ObjectId字段)的哈希值。 I want to use RANGE sharding with 'id' field. 我想在“ id”字段中使用RANGE分片。 The question is the following: 问题如下:

How can I set ranges for each shard when 'shardKey' is some long String (for example 64 chars)? 当'shardKey'是一些长字符串(例如64个字符)时,如何设置每个分片的范围?

If you want your data to be distributed based on a hash key, MongoDB has a built-in way of doing that: 如果您希望基于哈希键来分发数据,MongoDB具有一种内置方法:

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. 如果要随机分配数据,请使用{ fieldName: "hashed" }作为分片键定义。
  • If you want to manually control how data is distributed and accessed, use a normal shard key and define shard tags . 如果要手动控制数据的分发和访问方式,请使用普通的分片键并定义分片标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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