Ranged Sharding

Ranged-based sharding involves dividing data into contiguous ranges determinedby the shard key values. In this model, documents with “close” shard keyvalues are likely to be in the same chunk or shard. Thisallows for efficient queries where reads target documents within a contiguousrange. However, both read and write performance may decrease with poor shardkey selection. See Shard Key Selection.

Diagram of the shard key value space segmented into smaller ranges or chunks.

Range-based sharding is the default sharding methodology if no other optionssuch as those required for Hashed Sharding orzones are configured.

Shard Key Selection

Ranged sharding is most efficient when the shard key displays the followingtraits:

The following image illustrates a sharded cluster using the field X as theshard key. If the values for X have a large range, low frequency, andchange at a non-monotonic rate, the distribution of inserts may look similarto the following:

Diagram of good shard key distribution

Shard a Collection

Use the sh.shardCollection() method, specifying the full namespaceof the collection and the target index or compound indexto use as the shard key.

  1. sh.shardCollection( "database.collection", { <shard key> } )

Important

  • Once you shard a collection, the selection of the shard key isimmutable; i.e. you cannot select a different shard key for thatcollection.

  • Starting in MongoDB 4.2, you can update a document’s shard key valueunless the shard key field is the immutable _id field. For detailson updating the shard key, see Change a Document’s Shard Key Value.

Before MongoDB 4.2, a document’s shard key field value is immutable.

Shard a Populated Collection

If you shard a populated collection:

  • The sharding operation creates the initial chunk(s) to cover theentire range of the shard key values. The number of chunks createddepends on the configured chunk size.
  • After the initial chunk creation, the balancer migrates these initialchunks across the shards as appropriate as well as manages the chunkdistribution going forward.

Shard an Empty Collection

If you shard an empty collection:

  • With no zones and zone ranges specified for theempty or non-existing collection:
    • The sharding operation creates a single empty chunk to cover theentire range of the shard key values.
    • After the initial chunk creation, the balancer migrates the initialchunk across the shards as appropriate as well as manages the chunkdistribution going forward.
  • With zones and zone ranges specified for theempty or a non-existing collection (Available starting in MongoDB4.0.3),
    • The sharding operation creates empty chunks for the defined zoneranges as well as any additional chunks to cover the entire rangeof the shard key values and performs an initial chunk distributionbased on the zone ranges. This initial creation and distribution ofchunks allows for faster setup of zoned sharding.
    • After the initial distribution, the balancer manages the chunkdistribution going forward.