Docs 菜单

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() in mongosh has the same result as consecutively running the shardCollection and reshardCollection commands.

sh.shardAndDistributeCollection()接受以下参数:

Parameter
类型
必要性
说明

namespace

字符串

必需

The namespace of the collection to shard in the format "<database>.<collection>".

key

文档

必需

指定一个或多个字段用作分片键的文档。

{ <field1>: <1|"hashed">, ... }

将字段值设置为以下任一项:

另请参阅: 分片键索引

unique

布尔

Optional

指定 true 以确保基础索引执行唯一约束。默认值为 false

When using hashed shard keys, you can't specify true.

options

文档

Optional

A document containing optional fields, including numInitialChunks and collation.

options 参数支持以下选项:

Parameter
类型
说明

numInitialChunks

整型

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 numInitialChunks parameter must result in less than 8192 per shard. Defaults to 1000 chunks per shard.

collation

文档

If the collection specified to shardAndDistributeCollection() has a default 排序规则, you must include a collation document with { locale : "simple" }, or the shardAndDistributeCollection() method fails.

presplitHashedZones

布尔

Specify true to perform initial chunk creation and distribution for an empty or non-existing collection based on the defined zones and zone ranges for the collection. For hashed sharding only.

shardAndDistributeCollection() with presplitHashedZones: true returns an error if any of the following are true:

timeseries

文档

Specify this option to create a new sharded time series collection.

要对现有的时间序列集合进行分片,请省略此参数。

When the collection specified to shardAndDistributeCollection is a time series collection and the timeseries option is not specified, MongoDB uses the values that define the existing time series collection to populate the timeseries field.

有关详细语法,请参阅时间序列选项。

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" }
}
)