Database Manual / Sharding / Shard Keys

Shard a Collection分片集合

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缺少分片键段

Documents in sharded collections can be missing the shard key fields. A missing shard key falls into the same range as a null-valued shard key. 分片集合中的文档可能缺少分片键字段。缺失的分片键与null值分片键属于同一范围。See Missing Shard Key Fields.请参见缺少的分片键段

Change a Document's Shard Key Value更改文档的分片键值

You can update a document's shard key value unless the shard key field is the immutable _id field.您可以更新文档的分片键值,除非分片键字段是不可变的_id字段。

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开始,您可以通过更改文档的分片键来重新分片集合

You can refine a shard key by adding a suffix field or fields to the existing shard key.您可以通过向现有分片键添加一个或多个后缀字段来细化分片键