Shard Keys分片键

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试图在集群中的分片之间均匀分布区块。

Diagram of the shard key value space segmented into smaller ranges or chunks.

The shard key has a direct relationship to the effectiveness of chunk distribution. 切块与切块的有效性有着直接的关系。See Choose a Shard Key.请参见选择分片键

Shard Key Indexes分片键索引

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.索引可以是分片键上的索引,也可以是复合索引,其中分片键是索引的前缀

  • If the collection is empty, sh.shardCollection() creates the index on the shard key if such an index does not already exists.如果集合为空,sh.shardCollection()将在分片键上创建索引(如果该索引尚不存在)。
  • If the collection is not empty, you must create the index first before using 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.如果删除了分片键的最后一个有效索引,则只需在分片键上重新创建索引即可恢复。

Unique Indexes唯一索引

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 index on the shard key分片键上的索引
  • a compound index where the shard key is a prefix以分片键为前缀复合索引
  • 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索引仅对每个分片强制唯一性约束。

    Important重要
    Uniqueness and the _id Index唯一性和_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:独特的索引约束意味着:

  • For a to-be-sharded collection, you cannot shard the collection if the collection has other unique indexes.对于要分片的集合,如果该集合具有其他唯一索引,则无法分片该集合。
  • For an already-sharded collection, you cannot create unique indexes on other fields.对于已分片的集合,不能在其他字段上创建唯一索引。
  • A unique index stores a null value for a document missing the indexed field; that is a missing index field is treated as another instance of a null index key value. 唯一索引为缺少索引字段的文档存储空值;也就是说,缺少的索引字段被视为null索引键值的另一个实例。For more information, see Unique Index and Missing Field.有关更多信息,请参阅唯一索引和缺失字段

To enforce uniqueness on the shard key values, pass the unique parameter as true to the sh.shardCollection() method:要在分片键值上强制唯一性,请将unique参数作为true传递给sh.shardCollection()方法:

  • If the collection is empty, sh.shardCollection() creates the unique index on the shard key if such an index does not already exist.如果集合为空,sh.shardCollection()会在shard键上创建唯一索引(如果该索引不存在)。
  • If the collection is not empty, you must create the index first before using 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.不能在哈希索引上指定唯一约束。

Missing Shard Key Fields缺少分片关键字段

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.要设置缺少的分片关键点字段,请参阅设置缺少的分片关键点字段

Chunk Range and 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}字段上,则:

Document Missing Shard Key文档缺少分片键Falls into Same Range As属于与相同的范围
{ x: "hello" }{ x: "hello", y: null }
{ y: "goodbye" }{ x: null, y: "goodbye" }
{ z: "oops" }{ x: null, y: null }

Read/Write Operations and Missing Shard Key Fields读/写操作和缺少分片键字段

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
←  mongosShard a Collection →