Operational Restrictions in Sharded Clusters

Sharding Operational Restrictions

Operations Unavailable in Sharded Environments

$where does not permit references to the db objectfrom the $where function. This is uncommon inun-sharded collections.

The geoSearch command is not supported in shardedenvironments.

Single Document Modification Operations in Sharded Collections

All updateOne(), removeOne(),and deleteOne() operations for a shardedcollection that specify the multi: false or justOne option must include theshard keyor the _id field in the query specification.updateOne(), removeOne(),and deleteOne() operations specifying multi: false or justOnein a sharded collection which do not contain either theshard key or the _id field return an error.

Unique Indexes in Sharded Collections

MongoDB does not support unique indexes across shards, except whenthe unique index contains the full shard key as a prefix of theindex. In these situations MongoDB will enforce uniqueness acrossthe full key, not a single field.

See

Unique Constraints on Arbitrary Fieldsfor an alternate approach.

Sharding Existing Collection Data Size

An existing collection can only be sharded if its size does not exceedspecific limits. These limits can be estimated based on the average size ofall shard key values, and the configured chunk size.

Important

These limits only apply for the initial sharding operation. Shardedcollections can grow to any size after successfully enabling sharding.

Use the following formulas to calculate the theoretical maximumcollection size.

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

Note

The maximum BSON document size is 16MB or 16777216 bytes.

All conversions should use base-2 scale, e.g. 1024 kilobytes = 1megabyte.

If maxCollectionSize is less than or nearly equal to the targetcollection, increase the chunk size to ensure successful initial sharding.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 thechunk 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 tosplit to the new size. SeeModify Chunk Size in a Sharded Cluster for instructions onmodifying chunk size.

This table illustrates the approximate maximum collection sizesusing the formulas described above:

Average Size of Shard Key Values512 bytes256 bytes128 bytes64 bytes
Maximum Number of Splits32,76865,536131,072262,144
Max Collection Size (64 MB Chunk Size)1 TB2 TB4 TB8 TB
Max Collection Size (128 MB Chunk Size)2 TB4 TB8 TB16 TB
Max Collection Size (256 MB Chunk Size)4 TB8 TB16 TB32 TB