定义
shardCollectionShards a collection to distribute its documents across shards. The
shardCollectioncommand must be run against theadmindatabase.提示
In
mongosh, this command can also be run through thesh.shardCollection()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
To run shardCollection, use the db.runCommand( { <command> } ) method.
该命令采用以下形式:
db.adminCommand( { shardCollection: "<database>.<collection>", key: { <field1>: <1|"hashed">, ... }, unique: <boolean>, numInitialChunks: <integer>, presplitHashedZones: <boolean>, collation: { locale: "simple" }, timeseries: <object> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 要分片的集合的命名空间,格式为 |
| 文档 | 指定一个或多个字段用作分片键的文档。
将字段值设置为以下任一项: shard key must be supported by an index. Unless the collection is empty, the index must exist prior to the 另请参阅分片键索引 |
| 布尔 | 指定 使用哈希分片键时,不能指定 |
| 整型 | 指定在使用哈希分片键对空集合进行分片时,要在集群中所有分片上创建的数据块初始数量。然后,MongoDB 在集群中创建并均衡数据块。 如果集合不为空或分片键不包含哈希字段,则操作将返回错误。
|
| 文档 | 可选。如果指定给 |
布尔 | ||
对象 |
时间序列选项
5.1版本新增。
To create a new time series collection that is sharded, specify the timeseries option to shardCollection.
timeseries 选项包含以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 必需。包含每个时间序列文档中日期的字段的名称。时间序列集合中的文档必须具有有效 BSON 日期,以作为 |
| 字符串 | 可选。包含每个时间序列文档中元数据的字段的名称。指定字段中的元数据应是用于标记一系列独一无二的文档的数据。元数据应该很少改变(如有)。指定字段的名称可能不是 虽然 |
| 字符串 | 可选。可能的值为:
默认情况下,MongoDB 将 手动设置 如果您指定 如果未指定 如果设置了 |
Considerations
分片键
虽然您可在后续更改分片键,但请务必仔细考虑分片键的选择,以免出现可扩展性与性能问题。
时间序列集合上的分片键
对时间序列集合进行分片时,您只能为分片键指定以下字段:
使用
metaField子字段
metaField使用
timeField
您可以在分片键中指定这些字段的组合。不允许在分片键模式中使用任何其他字段,包括 _id。
在您指定分片键时:
timeField必须是:在分片键模式的末尾
提示
避免仅指定 timeField 作为分片键。由于 timeField 是单调增加的,因此,可能导致所有写入都出现在集群中的单个数据段上。理想情况下,数据均匀分布在数据段之间。
要了解如何最好地选择分片键,请参阅:
哈希分片键
使用表单 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.
有关示例,请参阅为空集合或不存在的集合预先定义区域和区域范围。如果使用范围或单字段哈希分片键对集合进行分片,并且已为空集合定义了区域和区域范围,则 numInitialChunks 选项将不起作用。
要使用复合哈希索引对集合进行分片,请参阅区域分片和复合哈希索引。
区域分片和复合哈希索引
MongoDB 支持使用组合哈希索引对集合进行分片。在使用组合哈希分片键对空集合或不存在的集合进行分片时,需要满足额外的要求,MongoDB 才能执行初始数据块创建和分配。
The numInitialChunks option has no effect if zones and zone ranges have been defined for the empty collection and presplitHashedZones is false.
有关示例,请参阅为空集合或不存在的集合预先定义区域和区域范围。
唯一性
如果指定 unique: true:
If the collection is empty,
shardCollectioncreates the unique index on the shard key if such an index does not already exist.If the collection is not empty, you must create the index first before using
shardCollection.
尽管可以有以分片键为前缀的唯一复合索引,但如果使用unique参数,则集合必须在分片键上有唯一索引。
另请参阅分片集合和唯一索引
排序规则
If the collection has a default collation, the shardCollection 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 write concern of the shardCollection command and its helper sh.shardCollection().
例子
以下操作为 records 数据库中的 people 集合启用分片并使用 zipcode 字段作为分片键:
db.adminCommand( { shardCollection: "records.people", key: { zipcode: 1 } } )