sh.shardAndDistributeCollection()
定义
sh.shardAndDistributeCollection(namespace, key, unique, options)
Shards a collection and immediately redistributes the data using the provided 片键. The immediate redistribution of data allows for faster data movement and reduced impact to workloads.
重要
mongosh 方法
本页面提供
mongosh
方法的相关信息。这不是 特定于语言的驱动程序(例如 Node.js)的文档。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
Running
sh.shardAndDistributeCollection()
inmongosh
has the same result as consecutively running theshardCollection
andreshardCollection
commands.
参数
sh.shardAndDistributeCollection()
接受以下参数:
Parameter | 类型 | 必要性 | 说明 |
---|---|---|---|
| 字符串 | 必需 | The namespace of the collection to shard in the format
|
| 文档 | 必需 | |
| 布尔 | Optional | 指定 When using hashed shard keys, you can't
specify |
| 文档 | Optional | A document containing optional fields, including
|
options
参数支持以下选项:
Parameter | 类型 | 说明 |
---|---|---|
| 整型 | Specifies the initial number of chunks to create across all shards in
the cluster when sharding and resharding a collection. MongoDB then
creates and balances chunks across the cluster. The
|
| 文档 | If the collection specified to |
| 布尔 | Specify
|
| 文档 | Specify this option to create a new sharded time series collection. 要对现有的时间序列集合进行分片,请省略此参数。 When the collection specified to 有关详细语法,请参阅时间序列选项。 |
Considerations
The following factors can impact performance or the distribution of your data.
分片键
Although you can change your shard key later, carefully consider your shard key choice to optimize scalability and perfomance.
时间序列集合上的分片键
对时间序列集合进行分片时,您只能为分片键指定以下字段:
metaField
metaField
的子字段timeField
您可以在分片键中指定这些字段的组合。不允许在分片键模式中使用任何其他字段,包括 _id
。
在您指定分片键时:
提示
避免仅指定 timeField
作为分片键。由于 timeField
是单调增加的,因此,可能导致所有写入都出现在集群中的单个数据段上。理想情况下,数据均匀分布在数据段之间。
要了解如何最好地选择分片键,请参阅:
另请参阅:
哈希分片键
To specify a hashed shard key field, use field: "hashed"
.
注意
如果在创建哈希分片键集合时正在进行块迁移,则初始块分布可能会不均匀,直到负载均衡器自动平衡集合。
另请参阅:
区域分片和初始数据块分布
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. 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.
For an example, see 为空集合或不存在的集合预定义区域和区域范围. 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.
To shard a collection using a compound hashed index, see 使用复合哈希索引进行初始数据段分发.
使用复合哈希索引进行初始数据段分发
MongoDB 支持使用组合哈希索引对集合进行分片。在使用组合哈希分片键对空集合或不存在的集合进行分片时,需要满足额外的要求,MongoDB 才能执行初始数据块创建和分配。
The numInitialChunks
option has no effect if zones and zone ranges
have been defined for the empty collection and
presplitHashedZones
is false
.
For an example, see 为空集合或不存在的集合预定义区域和区域范围.
另请参阅:
唯一性
If you specify unique: true
, you must create the index
before using sh.shardAndDistributeCollection()
.
Although you can have a unique 复合索引 where the shard
key is a prefix, if you use the unique
parameter, the collection must have a unique index that is on the shard
key.
另请参阅:
排序规则
If the collection has a default 排序规则,
the sh.shardAndDistributeCollection
command must include a collation
parameter with the
value { 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.
无需为没有排序规则的集合指定 collation
选项。如果确实为没有排序规则的集合指定了排序规则选项,则它将不起作用。
写关注
mongos
uses "majority"
for the
写入安全机制 of the
shardCollection
command, its helper
sh.shardCollection()
, and the
sh.shardAndDistributeCollection()
method.
示例
The following examples show how you can use the
sh.shardAndDistributeCollection()
method with or without optional
parameters.
简易用法
A database named records
contains a collection named people
. The
following command shards the collection by the zipcode
field and
immediately redistributes the data in the records.people
collection:
sh.shardAndDistributeCollection("records.people", { zipcode: 1 } )
用法以及选项
The phonebook
database has a contacts
collection with no
default 排序规则. The
following example uses sh.shardAndDistributeCollection()
to shard and
redistribute the phonebook.contacts
collection with:
A 哈希片键 on the
last_name
field.5
initial chunks.simple
collation.
sh.shardAndDistributeCollection( "phonebook.contacts", { last_name: "hashed" }, false, { numInitialChunks: 5, collation: { locale: "simple" } } )