On this page本页内容
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
namespace | "<database>.<collection>" ). "<database>.<collection>" )。 |
key |
|
For more information on the sharding method, see 有关分片方法的更多信息,请参阅sh.shardCollection()
.sh.shardCollection()
。
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.要设置缺少的分片关键点字段,请参阅设置缺少的分片关键点字段。
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.有关更新分片键值的详细信息,请参阅更改文档的分片键值。
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及更早版本中,分片后不能更改分片键的选择。