On this page本页内容
shardCollection
Shards a collection to distribute its documents across shards. 对集合进行分片化,以在分片之间分发其文档。You must run 在运行enableSharding
on a database before running the shardCollection
command. shardCollection
命令之前,必须在数据库上运行enableSharding
。The 必须对shardCollection
command must be run against the admin
database.admin
数据库运行shardCollection
命令。
To run 要运行shardCollection
, use the db.runCommand( { <command> } )
method.shardCollection
,请使用db.runCommand( { <command> } )
方法。
shardCollection
has the following form:具有以下形式:
{ shardCollection: "<database>.<collection>", key: { <field1>: <1|"hashed">, ... }, unique: <boolean>, numInitialChunks: <integer>, presplitHashedZones: <boolean>, collation: { locale: "simple" }, timeseries: <object> }
shardCollection
has the following fields:具有以下字段:
shardCollection | string | <database>.<collection> . <database>.<collection> 的形式分片。 |
key | document |
|
unique | boolean |
|
numInitialChunks | integer |
|
collation | document | shardCollection has a default collation, you must include a collation document with { locale : "simple" } , or the shardCollection command fails. shardCollection 的集合具有默认排序规则,则必须包含带有{ locale : "simple" } 的排序规则文档,否则shardCollection 命令将失败。 |
presplitHashedZones | boolean |
|
timeseries | object |
|
New in version 5.1.在版本5.1中新增。
To create a new time series collection that is sharded, specify the timeseries option to 要创建新的分片时间序列集合,请将shardCollection
.timeseries
选项指定为shardCollection
。
The timeseries option takes the following fields:timeseries
选项包含以下字段:
timeField | string |
|
metaField | string |
|
granularity | string |
|
Do not run more than one 不要同时对同一集合运行多个shardCollection
command on the same collection at the same time.shardCollection
命令。
Once a collection has been sharded, MongoDB provides no method to unshard a sharded collection.一旦一个集合被分片,MongoDB就不提供任何方法来对分片的集合进行分片。
While you can change your shard key later, it is important to carefully consider your shard key choice to avoid scalability and perfomance issues.虽然您可以稍后更改分片键,但必须仔细考虑分片键的选择,以避免可伸缩性和性能问题。
When sharding time series collections, you can only specify the following fields in the shard key:对时间序列集合进行分片时,只能在分片键中指定以下字段:
metaField
metaField
metaField
metaField
的子字段timeField
timeField
You may specify combinations of these fields in the shard key. 您可以在分片键中指定这些字段的组合。No other fields, including 分片键模式中不允许包含_id
, are allowed in the shard key pattern._id
在内的其他字段。
When you specify the shard key:指定分片键时:
metaField
can be either a:可以是:
timeField
must be:必须是:
Avoid specifying only the 避免仅将timeField
as the shard key. timeField
指定为分片键。Since the 由于timeField
increases monotonically, it may result in all writes appearing on a single chunk within the cluster. timeField
单调增加,因此可能会导致所有写入出现在集群内的单个块上。Ideally, data is evenly distributed across chunks.理想情况下,数据在块之间均匀分布。
To learn how to best choose a shard key, see:要了解如何最佳选择分片键,请参阅:
Hashed shard keys use a hashed index or a compound hashed index as the shard key.哈希分片键使用哈希索引或复合哈希索引作为分片键。
Use the form 使用field: "hashed"
to specify a hashed shard key field.field: "hashed"
指定一个哈希分片键字段。
If chunk migrations are in progress while creating a hashed shard key collection, the initial chunk distribution may be uneven until the balancer automatically balances the collection.如果在创建哈希分片键集合时正在进行块迁移,则在平衡器自动平衡集合之前,初始块分布可能不均匀。
The shard collection operation (i.e. 如果已为集合定义了区域和区域范围,则分片集合操作(即shardCollection
command and the sh.shardCollection()
helper) can perform initial chunk creation and distribution for an empty or a non-existing collection if zones and zone ranges have been defined for the collection. shardCollection
命令和sh.shardCollection()
助手)可以为空集合或不存在的集合执行初始块创建和分发。Initial chunk distribution allows for a faster setup of zoned sharding. 初始块分布允许更快地设置分区分片。After the initial distribution, the balancer manages the chunk distribution going forward per usual.在初始分发之后,平衡器按照惯例管理块分发。
See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example. 有关示例,请参阅为空集合或非现有集合预先定义分区和分区范围。If sharding a collection using a ranged or single-field hashed shard key, the 如果使用范围或单个字段哈希分片键对集合进行分片化,则如果为空集合定义了区域和区域范围,numInitialChunks
option has no effect if zones and zone ranges have been defined for the empty collection.numInitialChunks
选项将无效。
To shard a collection using a compound hashed index, see Zone Sharding and Compound Hashed Indexes.要使用复合哈希索引对集合进行分片化,请参阅区域分片化和复合哈希索引。
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. 从4.4版开始,MongoDB支持在复合哈希索引上对集合进行分片。When sharding an empty or non-existing collection using a compound hashed shard key, additional requirements apply in order for MongoDB to perform initial chunk creation and distribution.当使用复合散列分片键对空的或不存在的集合进行分片时,为了让MongoDB执行初始块创建和分发,还需要额外的要求。
The 如果为空集合定义了区域和区域范围,并且numInitialChunks
option has no effect if zones and zone ranges have been defined for the empty collection andpresplitHashedZones is false
.resplitHashedZones
为false
,则numInitialChunks
选项无效。
See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example.有关示例,请参阅为空集合或非现有集合预先定义分区和分区范围。
If specifying 如果指定unique: true
:unique: true
:
shardCollection
creates the unique index on the shard key if such an index does not already exist.shardCollection
将在shard键上创建唯一索引(如果该索引不存在)。shardCollection
.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.unique
参数,则集合必须在分片键上具有唯一索引。
See also Sharded Collection and Unique Indexes另请参见分片集合和唯一索引
Changed in version 3.4.在版本3.4中更改。
If the collection has a default collation, the 如果集合具有默认排序规则,则shardCollection
command must include a collation
parameter with the value { locale: "simple" }
. shardCollection
命令必须包含值为{ locale: "simple" }
的排序规则参数。For non-empty collections with a default 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. collation
的集合指定排序规则选项。If you do specify the collation option for a collection with no collation, it will have no effect.如果确实为没有排序规则的集合指定排序规则选项,则该选项将无效。
mongos
uses "majority"
for the write concern of the shardCollection
command and its helper sh.shardCollection()
.mongos
使用"majority"
表示shardCollection
命令及其助手sh.shardCollection()
的写入关注点。
The following operation enables sharding for the 以下操作启用people
collection in the records
database and uses the zipcode
field as the shard key:records
数据库中people
集合的分片,并使用zipcode
字段作为分片键:
db.adminCommand( { shardCollection: "records.people", key: { zipcode: 1 } } )