On this page本页内容
The shard key is either a single indexed field or multiple fields covered by a compound index that determines the distribution of the collection's documents among the cluster's shards.分片键可以是单个索引字段,也可以是复合索引覆盖的多个字段,复合索引确定集合文档在集群分片中的分布。
MongoDB divides the span of shard key values (or hashed shard key values) into non-overlapping ranges of shard key values (or hashed shard key values). MongoDB将分片键值(或散列分片键值)的范围划分为不重叠的分片键值(或散列分片键值)范围。Each range is associated with a chunk, and MongoDB attempts to distribute chunks evenly among the shards in the cluster.每个范围都与一个区块相关联,MongoDB试图在集群中的分片之间均匀分布区块。
The shard key has a direct relationship to the effectiveness of chunk distribution. 切块与切块的有效性有着直接的关系。See Choose a Shard Key.请参见选择分片键。
All sharded collections must have an index that supports the shard key. 所有分片集合都必须有一个支持分片键的索引。The index can be an index on the shard key or a compound index where the shard key is a prefix of the index.索引可以是分片键上的索引,也可以是复合索引,其中分片键是索引的前缀。
sh.shardCollection()
creates the index on the shard key if such an index does not already exists.sh.shardCollection()
将在分片键上创建索引(如果该索引尚不存在)。sh.shardCollection()
.sh.shardCollection()
。If you drop the last valid index for the shard key, recover by recreating an index on just the shard key.如果删除了分片键的最后一个有效索引,则只需在分片键上重新创建索引即可恢复。
MongoDB can enforce a uniqueness constraint on a ranged shard key index. MongoDB可以对范围内的分片键索引实施唯一性约束。Through the use of a unique index on the shard key, MongoDB enforces uniqueness on the entire key combination and not individual components of the shard key.通过对分片键使用唯一索引,MongoDB对整个键组合而不是分片键的单个组件强制执行唯一性。
For a ranged sharded collection, only the following indexes can be unique:对于范围内的分片集合,只有以下索引可以是唯一的:
the default 默认的_id
index; however, the _id
index only enforces the uniqueness constraint per shard if the _id
field is not the shard key or the prefix of the shard key._id
索引;但是,如果_id
字段不是分片键或分片键的前缀,则_id
索引仅对每个分片强制唯一性约束。
_id
索引If the 如果_id
field is not the shard key or the prefix of the shard key, _id
index only enforces the uniqueness constraint per shard and not across shards._id
字段不是分片键或分片键的前缀,则_id
索引仅对每个分片强制唯一性约束,而不是跨多个分片强制唯一性约束。
For example, consider a sharded collection (with shard key 例如,考虑一个分片集合(具有分片键{x: 1}
) that spans two shards A and B. {x:1}
),它跨越两个分片a和B。Because the 因为_id
key is not part of the shard key, the collection could have a document with _id
value 1
in shard A and another document with _id
value 1
in shard B._id
键不是分片键的一部分,所以集合可以有一个在分片A中具有_id
值1的文档,以及另一个在分片B中具有_id
值1的文档。
If the 如果_id
field is not the shard key nor the prefix of the shard key, MongoDB expects applications to enforce the uniqueness of the _id
values across the shards._id
字段既不是分片键,也不是分片键的前缀,MongoDB希望应用程序在各个shard上强制执行_id
值的唯一性。
The unique index constraints mean that:独特的索引约束意味着:
null
index key value. null
索引键值的另一个实例。To enforce uniqueness on the shard key values, pass the 要在分片键值上强制唯一性,请将unique
parameter as true
to the sh.shardCollection()
method:unique
参数作为true
传递给sh.shardCollection()
方法:
sh.shardCollection()
creates the unique index on the shard key if such an index does not already exist.sh.shardCollection()
会在shard键上创建唯一索引(如果该索引不存在)。sh.shardCollection()
.sh.shardCollection()
。Although you can have a unique compound index where the shard key is a prefix, if using 虽然可以使用以分片键为前缀的唯一复合索引,但如果使用unique
parameter, the collection must have a unique index that is on the shard key.unique
参数,集合必须在分片键上具有唯一索引。
You cannot specify a unique constraint on a hashed index.不能在哈希索引上指定唯一约束。
Starting in version 4.4, documents in sharded collections can be missing the shard key fields. 从4.4版开始,分片集合中的文档可能会缺少分片键字段。To set missing shard key fields, see Set Missing Shard Key Fields.要设置缺少的分片关键点字段,请参阅设置缺少的分片关键点字段。
Missing shard key fields fall within the same chunk range as shard keys with null values. For example, if the shard key is on the fields 缺少的分片键字段与具有空值的分片键属于同一块范围。例如,如果分片键位于{ x: 1, y: 1 }
, then:{x:1, y:1}
字段上,则:
{ x: "hello" } | { x: "hello", y: null } |
{ y: "goodbye" } | { x: null, y: "goodbye" } |
{ z: "oops" } | { x: null, y: null } |
To target documents with missing shard key fields, you can use the 要针对缺少分片键字段的文档,可以对分片键字段使用{ $exists: false }
filter condition on the shard key fields. { $exists: false }
筛选条件。For example, if the shard key is on the fields 例如,如果分片键位于{ x: 1, y: 1 }
, you can find the documents with missing shard key fields by running this query:{ x: 1, y: 1 }
字段上,则可以通过运行以下查询来查找缺少分片键字段的文档:
db.shardedcollection.find( { $or: [ { x: { $exists: false } }, { y: { $exists: false } } ] } )
If you specify a null equality match filter condition (e.g. 如果指定空相等匹配筛选器条件(例如{ x: null }
), the filter matches both those documents with missing shard key fields and those with shard key fields set to null
.{ x: null }
),筛选器将匹配缺少分片键字段的文档和分片键字段设置为空的文档。
Some write operations, such as a write with an 某些写入操作,例如使用upsert
specification, require an equality match on the shard key. upsert
规范的写入,需要在分片键上进行相等匹配。In those cases, to target a document that is missing the shard key, include another filter condition in addition to the 在这些情况下,要将缺少分片键的文档作为目标,除了null
equality match.null
相等匹配之外,还要包括另一个筛选条件。 For example:
{ _id: <value>, <shardkeyfield>: null } // _id of the document missing shard key