Shard a Collection分片集合
On this page本页内容
To shard a collection, you must specify the full namespace of the collection that you want to shard and the shard key. 要对集合进行分片,必须指定要分片的集合的完整命名空间和分片键。You can use the 您可以使用mongosh
method sh.shardCollection()
to shard a collection:mongosh
方法sh.shardCollection()
对集合进行分片:
sh.shardCollection(<namespace>, <key>) //Optional parameters omitted省略了可选参数
namespace | "<database>.<collection>" )."<database>.<collection>" )。 |
key | { <shard key field1>: <1|"hashed">, ... } where{ <shard key field1>: <1|"hashed">, ... } ,其中
|
For more information on the sharding method, see 有关分片方法的更多信息,请参阅sh.shardCollection()
.sh.shardCollection()
。
Shard Key Fields and Values分片键字段和值
Missing Shard Key Fields缺少分片键字段
Starting in version 4.4, documents in sharded collections can be missing the shard key fields. 从4.4版本开始,分片集合中的文档可能会缺少分片键字段。A missing shard key falls into the same range as a 缺失的分片键与null
-valued shard key. null
值分片键属于相同的范围。See Missing Shard Key Fields.请参见缺少分片键字段。
In version 4.2 and earlier, shard key fields must exist in every document to be able to shard a sharded collection. To set missing shard key fields, see Set Missing Shard Key Fields.在4.2及更早版本中,每个文档中都必须存在分片键字段,才能对分片集合进行分片。若要设置丢失的分片关键帧字段,请参阅设置丢失的片段关键帧字段。
Change a Document's Shard Key Value更改文档的分片键值
Starting in MongoDB 4.2, you can update a document's shard key value unless the shard key field is the immutable 从MongoDB 4.2开始,您可以更新文档的分片键值,除非分片键字段是不可变的_id
field. _id
字段。In MongoDB 4.2 and earlier, a document's shard key field value is immutable.在MongoDB 4.2及更早版本中,文档的分片键字段值是不可变的。
For details on updating the shard key value, see Change a Document's Shard Key Value.有关更新分片键值的详细信息,请参阅更改文档的分片键值。
Change a Collection's Shard Key更改集合的分片键
Starting in MongoDB 5.0, you can reshard a collection by changing a document's shard key.从MongoDB 5.0开始,您可以通过更改文档的分片键来重新分片集合。
Starting in MongoDB 4.4, you can refine a shard key by adding a suffix field or fields to the existing shard key.从MongoDB 4.4开始,您可以通过在现有的分片键中添加一个或多个后缀字段来细化分片键。
In MongoDB 4.2 and earlier, the choice of shard key cannot be changed after sharding.在MongoDB 4.2及更早版本中,分片后不能更改对分片键的选择。