Docs HomeMongoDB Manual

Shard Keys分片键

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

You cannot drop or hide an index if it is the only non-hidden index that supports 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. 例如,考虑一个跨越两个分片a和B的分片集合(具有分片键{x: 1})。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希望应用程序在分片之间强制执行_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()会在分片键上创建唯一索引(如果该索引不存在)。
  • 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:缺少的分片键字段与具有null值的分片键属于相同的区块范围。例如,如果分片键位于字段{ 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.如果指定了null相等匹配筛选器条件(例如{ x: null }),则筛选器将匹配那些缺少分片键字段的文档和那些将分片键域设置为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