Operational Restrictions in Sharded Clusters分片集群中的操作限制

On this page本页内容

Sharding Operational Restrictions分片操作限制

Operations Unavailable in Sharded Environments在分片环境中不可用的操作

$where does not permit references to the db object from the $where function. 不允许从$where函数引用db对象。This is uncommon in un-sharded collections.这在未分片的集合中很少见。

The geoSearch command is not supported in sharded environments.分片环境中不支持geoSearch命令。

Single Document Modification Operations in Sharded Collections分片集合中的单个文档修改操作

All updateOne() and deleteOne() operations for a sharded collection that specify the multi: false or justOne option must include the shard key or the _id field in the query specification.对于指定multi:false或justOne选项的分片集合,所有updateOne()deleteOne()操作都必须在查询规范中包含分片键_id字段。

updateOne() and deleteOne() operations specifying multi: false or justOne in a sharded collection which do not contain either the shard key or the _id field return an error.updateOne()deleteOne()操作在不包含分片键或_id字段的分片集合中指定multi:falsejustOne,将返回错误。

To use findOneAndUpdate() with a sharded collection, your query filter must include an equality condition on the shard key to compare the key and value in either of these formats:要对分片集合使用findOneAndUpdate(),查询筛选器必须在分片键上包含相等条件,以比较以下任一格式的键和值:

{ key: value }
{ key: { $eq: value } }

Unique Indexes in Sharded Collections分片集合中的唯一索引

MongoDB does not support unique indexes across shards, except when the unique index contains the full shard key as a prefix of the index. MongoDB不支持跨分片的唯一索引,除非唯一索引包含完整的分片键作为索引的前缀。In these situations MongoDB will enforce uniqueness across the full key, not a single field.在这些情况下,MongoDB将在整个密钥而不是单个字段中强制唯一性。

Tip提示
See: 参阅:

Unique Constraints on Arbitrary Fields任意字段上的唯一约束 for an alternate approach.以备不时之需。

Sharding Existing Collection Data Size分片现有集合数据大小

An existing collection can only be sharded if its size does not exceed specific limits. 只有当现有集合的大小不超过特定限制时,才能对其进行分片。These limits can be estimated based on the average size of all shard key values, and the configured chunk size.可以根据所有分片键值的平均大小和配置的大小来估计这些限制。

Important重要

These limits only apply for the initial sharding operation. 这些限制仅适用于初始分片操作。Sharded collections can grow to any size after successfully enabling sharding.成功启用分片后,分片集合可以增长到任何大小。

Use the following formulas to calculate the theoretical maximum collection size.使用以下公式计算理论最大集合大小。

maxSplits = 16777216 (bytes) / <average size of shard key values in bytes>
maxCollectionSize (MB) = maxSplits * (chunkSize / 2)
Note注意

The maximum BSON document size is 16MB or 16777216 bytes.BSON文档的最大大小为16MB或16777216字节。

All conversions should use base-2 scale, e.g. 1024 kilobytes = 1 megabyte.所有转换应使用基数2,例如1024 KB=1 MB。

If maxCollectionSize is less than or nearly equal to the target collection, increase the chunk size to ensure successful initial sharding. 如果maxCollectionSize小于或几乎等于目标集合,请增加区块大小以确保初始分片成功。If there is doubt as to whether the result of the calculation is too 'close' to the target collection size, it is likely better to increase the chunk size.如果怀疑计算结果是否过于“接近”目标集合大小,则最好增加区块大小。

After successful initial sharding, you can reduce the chunk size as needed. 在成功进行初始分片后,可以根据需要减小块大小。If you later reduce the chunk size, it may take time for all chunks to split to the new size. 如果以后减小块大小,则可能需要一些时间才能将所有块拆分为新大小。See Modify Chunk Size in a Sharded Cluster for instructions on modifying chunk size.有关修改区块大小的说明,请参阅修改分片集群中的区块大小

This table illustrates the approximate maximum collection sizes using the formulas described above:此表使用上述公式说明了近似的最大集合大小:

Average Size of Shard Key Values分片键值的平均大小512 bytes256 bytes128 bytes64 bytes
Maximum Number of Splits最大拆分数32,76865,536131,072262,144
Max Collection Size (64 MB Chunk Size)最大集合大小(64 MB区块大小)1 TB2 TB4 TB8 TB
Max Collection Size (128 MB Chunk Size)最大集合大小(128 MB区块大小)2 TB4 TB8 TB16 TB
Max Collection Size (256 MB Chunk Size)最大集合大小(256 MB区块大小)4 TB8 TB16 TB32 TB
←  Sharding ReferenceTroubleshoot Sharded Clusters →