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试图在集群中的分片之间均匀地分配区块。
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
索引仅强制每个分片的唯一性约束。ImportantUniqueness 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例如,考虑一个跨越两个分片a和B的分片集合(具有分片键{x: 1}
) that spans two shards A and B.{x: 1}
)。Because the因为_id
key is not part of the shard key, the collection could have a document with_id
value1
in shard A and another document with_id
value1
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 }
上,则:
{ 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