Shard a Collection分片一个集合

On this page本页内容

Note注意

To shard a collection, you need to enable sharding.要分片集合,需要启用分片

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
namespaceSpecify the full namespace of the collection that you want to shard ("<database>.<collection>"). 指定要分片的集合的完整命名空间("<database>.<collection>")。
key

Specify a document { <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. 在4.2版及更早版本中,每个文档中都必须存在分片键字段,才能分片分片的集合。To set missing shard key fields, see Set Missing Shard Key Fields.要设置缺少的分片关键点字段,请参阅设置缺少的分片关键点字段

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 _id field. 从MongoDB 4.2开始,您可以更新文档的分片键值,除非分片键字段是不可变的_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及更早版本中,分片后不能更改分片键的选择。

←  Shard KeysChoose a Shard Key →