On this page本页内容
sh.shardCollection(namespace, key, unique, options) Shards a collection using the 使用key as a the shard key. key作为分片键来分割集合。The shard key determines how MongoDB distributes the collection's documents among the shards.分片键决定MongoDB如何在分片之间分发集合的文档。
The mongosh method sh.shardCollection() wraps the shardCollection command.mongosh方法sh.shardCollection()包装了shardCollection命令。
sh.shardCollection() takes the following arguments:采用以下参数:
namespace | string | "<database>.<collection>". "<database>.<collection>"。 |
key | document |
|
unique | boolean |
|
options | document | numInitialChunks and collation. numInitialChunks和collation。 |
The options argument supports the following options:options参数支持以下选项:
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 | document |
|
New in version 5.1.在版本5.1中新增。
To create a new time series collection that is sharded, specify the timeseries option to 要创建分片的新时间序列集合,请将sh.shardCollection().timeseries选项指定给sh.shardCollection()。
The timeseries option takes the following fields:timeseries选项采用以下字段:
timeField | string |
|
metaField | string |
|
granularity | string |
|
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:对时间序列集合进行分片时,只能在分片键中指定以下字段:
metaFieldmetaFieldmetaFieldmetaField的子字段timeFieldYou 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"指定一个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命令和shshardCollection()助手)可以为空集合或不存在的集合执行初始区块创建和分发。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 Initial Chunk Distribution with 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.presplitHashedZones为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:
sh.shardCollection() creates the unique index on the shard key if such an index does not already exist.sh.shardCollection()会在尚不存在的分片键上创建唯一索引。sh.shardCollection().sh.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 如果集合具有默认排序规则,则sh.shardCollection() command must include a collation parameter with the value { locale: "simple" }. sh.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().
Given a collection named 在名为records的数据库中,给定一个名为people in a database named records, the following command shards the collection by the zipcode field:people的集合,以下命令通过zipcode字段对该集合进行分片:
sh.shardCollection("records.people", { zipcode: 1 } )
The phonebook database has a collection contacts with no default collation. phonebook数据库有一个没有默认排序规则的集合contacts。The following example uses 以下示例使用sh.shardCollection() to shard the phonebook.contacts with:sh.shardCollection()将phonebook.contacts分片为:
last_name field,last_name字段上的散列分片键,5simple.simple的排序规则。sh.shardCollection( "phonebook.contacts", { last_name: "hashed" }, false, { numInitialChunks: 5, collation: { locale: "simple" } } )