shardCollection

On this page本页内容

Definition定义

shardCollection

Shards a collection to distribute its documents across shards. 对集合进行分片化,以在分片之间分发其文档。You must run enableSharding on a database before running the shardCollection command. 在运行shardCollection命令之前,必须在数据库上运行enableShardingThe 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:具有以下字段:

Field字段Type类型Description描述
shardCollectionstringThe namespace of the collection to shard in the form <database>.<collection>. 集合的名称空间<database>.<collection>的形式分片。
keydocument

The document that specifies the field or fields to use as the shard key.指定要用作分片键的一个或多个字段的文档。

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

Set the field values to either:将字段值设置为:

shard key片键 must be supported by an index. 必须由索引支持。Unless the collection is empty, the index must exist prior to the shardCollection command. 除非集合为空,否则索引必须在shardCollection命令之前存在。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.如果集合为空,如果可以支持分片键的索引不存在,MongoDB将在分片集合之前创建索引。

See also Shard Key Indexes另请参见共享密钥索引

uniqueboolean

Specify true to ensure that the underlying index enforces a unique constraint. 指定true以确保基础索引强制执行唯一约束。Defaults to false.默认为false

You cannot specify true when using hashed shard keys.使用哈希分片键时不能指定true

numInitialChunksinteger

Specifies the initial number of chunks to create across all shards in the cluster when sharding an empty collection with a hashed shard key. MongoDB will then create and balance chunks across the cluster. 指定当使用哈希分片键对空集合进行分片化时,要在集群中的所有分片中创建的初始块数。然后MongoDB将在集群中创建和平衡区块。The numInitialChunks must result in less than 8192 per shard.numInitialChunks必须导致每个分片少于8192个。

If the collection is not empty or the shard key does not contain a hashed field, the operation returns an error.如果集合不为空或分片键不包含哈希字段,则操作返回错误。

  • If sharding with presplitHashedZones: true, MongoDB attempts to evenly distribute the specified number of chunks across the zones in the cluster.如果使用presplitHashedZones: true进行分片,MongoDB将尝试在集群中的区域中均匀分布指定数量的块。
  • If sharding with presplitHashedZones: false or omitted and no zones and zone ranges are defined for the empty collection, MongoDB attempts to evenly distributed the specified number of chunks across the shards in the cluster.如果使用presplitHashedZones: false或省略进行分片,并且没有为空集合定义区域和区域范围,MongoDB将尝试在集群中的分片上均匀分布指定数量的块。
  • If sharding with presplitHashedZones: false or omitted and zones and zone ranges have been defined for the empty collection, numInitChunks has no effect.如果使用presplitHashedZones: false或省略进行分片,并且为空集合定义了区域和区域范围,则numInitChunks无效。

Changed in version 4.4.在版本4.4中更改

collationdocumentOptional. 可选。If the collection specified to shardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the shardCollection command fails. 如果指定给shardCollection的集合具有默认排序规则,则必须包含带有{ locale : "simple" }的排序规则文档,否则shardCollection命令将失败。At least one of the indexes whose fields support the shard key pattern must have the simple collation. 字段支持分片键模式的索引中至少有一个索引必须具有简单排序规则。
presplitHashedZonesboolean

Optional. 可选。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. 指定true以根据集合的定义区域和区域范围为空集合或不存在集合执行初始区块创建和分发。For hashed sharding only.仅用于哈希分片

shardCollection with presplitHashedZones: true returns an error if any of the following are true:带有presplitHashedZones: trueshardCollection如果以下任何一项为true,则返回错误:

New in version 4.4.在版本4.4中新增

timeseriesobject

Optional. 可选。Specify this option to create a new sharded time series collection.指定此选项以创建新的分片时间序列集合

To shard an existing time series collection, omit this parameter.若要对现有时间序列集合进行分片,请忽略此参数。

When the collection specified to shardCollection 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.当指定给shardCollection的集合是时间序列集合并且未指定timeseries选项时,MongoDB使用定义现有时间序列集合的值来填充timeseries字段。

For detailed syntax, see Time Series Options.有关详细语法,请参阅时间序列选项

New in version 5.1.在版本5.1中新增

Time Series Options时间序列选项

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选项包含以下字段:

Field字段Type类型Description描述
timeFieldstring

Required. 必要。The name of the field which contains the date in each time series document. 包含每个时间序列文档中日期的字段的名称。Documents in a time series collection must have a valid BSON date as the value for the timeField.时间序列集合中的文档必须具有有效的BSON日期作为timeField的值。

metaFieldstring

Optional. 可选。The name of the field which contains metadata in each time series document. 每个时间序列文档中包含元数据的字段的名称。The metadata in the specified field should be data that is used to label a unique series of documents. 指定字段中的元数据应该是用于标记一系列唯一文档的数据。The metadata should rarely, if ever, change.元数据应该很少改变(如果有的话)。

The name of the specified field may not be _id or the same as the timeseries.timeField. 指定字段的名称不能是_id,也不能与timeseries.timeField相同。The field can be of any type.字段可以是任何类型。

granularitystring

Optional. 可选。Possible values are:可能的值包括:

  • "seconds"
  • "minutes"
  • "hours"

By default, MongoDB sets the granularity to "seconds" for high-frequency ingestion.默认情况下,MongoDB将高频摄取的granularity设置为"seconds"

Manually set the granularity parameter to improve performance by optimizing how data in the time series collection is stored internally. 手动设置granularity参数,通过优化时间序列集合中数据的内部存储方式来提高性能。To select a value for granularity, choose the closest match to the time span between consecutive incoming measurements.要选择granularity值,请选择与连续传入测量之间的时间跨度最接近的匹配。

If you specify the timeseries.metaField, consider the time span between consecutive incoming measurements that have the same unique value for the metaField field. 如果指定timeseries.metaField,请考虑具有相同唯一metaField字段值的连续传入测量之间的时间跨度。Measurements often have the same unique value for the metaField field if they come from the same source.如果测量来自同一源,则metaField字段通常具有相同的唯一值。

If you do not specify timeseries.metaField, consider the time span between all measurements that are inserted in the collection.如果未指定timeseries.metaField,请考虑插入集合中的所有测量值之间的时间跨度。

Considerations注意事项

Use用法

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就不提供任何方法来对分片的集合进行分片。

Shard Keys分片键

While you can change your shard key later, it is important to carefully consider your shard key choice to avoid scalability and perfomance issues.虽然您可以稍后更改分片键,但必须仔细考虑分片键的选择,以避免可伸缩性和性能问题。

Shard Keys on Time Series Collections时间序列集合上的分片键

When sharding time series collections, you can only specify the following fields in the shard key:对时间序列集合进行分片时,只能在分片键中指定以下字段:

  • The metaFieldmetaField
  • Sub-fields of metaFieldmetaField的子字段
  • The timeFieldtimeField

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:指定分片键时:

Tip提示

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哈希分片键

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"指定一个哈希分片键字段。

Note注意

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.如果在创建哈希分片键集合时正在进行块迁移,则在平衡器自动平衡集合之前,初始块分布可能不均匀。

Tip提示
See also: 参阅:

Zone Sharding and Initial Chunk Distribution区域分割和初始块分布

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.要使用复合哈希索引对集合进行分片化,请参阅区域分片化和复合哈希索引

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.如果为空集合定义了区域和区域范围,并且resplitHashedZonesfalse,则numInitialChunks选项无效。

See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example.有关示例,请参阅为空集合或非现有集合预先定义分区和分区范围

Uniqueness唯一性

If specifying unique: true:如果指定unique: true

  • If the collection is empty, shardCollection creates the unique index on the shard key if such an index does not already exist.如果集合为空,则shardCollection将在shard键上创建唯一索引(如果该索引不存在)。
  • If the collection is not empty, you must create the index first before using 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另请参见分片集合和唯一索引

Collation排序规则

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.如果确实为没有排序规则的集合指定排序规则选项,则该选项将无效。

Write Concern写入关注

mongos uses "majority" for the write concern of the shardCollection command and its helper sh.shardCollection().mongos使用"majority"表示shardCollection命令及其助手sh.shardCollection()写入关注点

Example示例

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 } } )
←  setShardVersionshardingState →