翻译或纠错本页面

分片集合

On this page

定义

shardCollection

开启一个集合的分片,之后MongoDB就可以在分片间分配这个集合的数据.在运行 shardCollection 命令之前必须先在集合所在的数据库上运行 enableSharding 命令, shardCollection 使用以下格式:

To run shardCollection, use the db.runCommand( { <command> } ) method.

shardCollection has the following form:

{
   shardCollection: "<database>.<collection>",
   key: <shardkey>,
   unique: <boolean>,
   numInitialChunks: <integer>,
   collation: { locale: "simple" }
}

命令 shardCollection 有以下字段:

Field Type Description
shardCollection string The namespace of the collection to shard in the form <database>.<collection>.
key document

The index specification document to use as the shard key. The shard key determines how MongoDB distributes the documents among the shards.

Unless the collection is empty, the index must exist prior to the shardCollection command. If the collection is empty, MongoDB creates the index prior to sharding the collection if the index that can support the shard key does not already exist.

See also Shard Key Indexes

unique boolean When true, the unique option ensures that the underlying index enforces a unique constraint. Hashed shard keys do not support unique constraints.
numInitialChunks integer Specifies the number of chunks to create initially when sharding an empty collection with a hashed shard key. MongoDB will then create and balance chunks across the cluster. The numInitialChunks must be less than 8192 per shard. If the collection is not empty, numInitialChunks has no effect.
collation document Optional. If the collection specified to shardCollection has a default collation, you must set shardCollection to { locale : "simple"}, or the shardCollection command fails. At least one of the indexes whose fields support the shard key pattern must have the simple collation.

注意事项

使用

You can only shard a collection once.

Do not run more than one shardCollection command on the same collection at the same time.

在执行 shardCollection 之后,MongoDB没有提供取消集群分片的方法.另外,执行 shardCollection 之后,集合的片键与文档中的片键值也不能够被修改.

片键

如何选择一个好的片键,以使得集群中的分片负载均衡较好,需要一些规划.参见 片键 来选择一个好的片键.

哈希片键

Hashed shard keys use a hashed index of a single field as the shard key.

Use the form {field: "hashed"} to specify a hashed shard key. Hashed shard keys may not be compound indexes.

注解

如果在创建哈希片键时,数据块正在迁移,那么在自动均衡过程使得到集合均衡之前,初始化的数据块可能不均衡.

Uniqueness

If specifying unique: true:

  • 以下的操作在 records 数据库中的 people 集合上使用 zipcode 作为片键对此集合开启了分片.

  • If the collection is not empty, you must create the index first before using 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.

See also Sharded Collection and Unique Indexes

Collation

在 3.4 版更改.

If the collection has a default collation, your shardCollection command must include a collation parameter with the value { locale: "simple" }. For non-empty collections with a collation, you must have at least one index with the simple collation whose fields support the shard key pattern.

You do not need to specify the collation option for collections without a collation. If you do specify the collation option for a collection with no collation, it will have no effect.

Example

The following operation enables sharding for the people collection in the records database and uses the zipcode field as the shard key:

use admin
db.runCommand( { shardCollection: "records.people", key: { zipcode: 1 } } )

参见

分片