<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Hashed Sharding
  <a class="headerlink leafygreen-ui-1kepx47" href="#hashed-sharding" title="Permalink to this heading">
   <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
    <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
    </path>
    <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
    </path>
   </svg>
   <div class="css-1btu0w3 evvkmar0" id="hashed-sharding">
   </div>
  </a>
 </h1>
 <p class="leafygreen-ui-1kp3ins">
  Hashed sharding uses either a
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-hashed-index">
   single field hashed index
  </a>
  or a
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/create/#std-label-index-type-compound-hashed">
   compound hashed index
  </a>
  as the shard key to
partition data across your sharded cluster.
 </p>
 <dl>
  <dt>
   Sharding on a Single Field Hashed Index
  </dt>
  <dd class="css-1ufxuu7">
   <p class="leafygreen-ui-1kp3ins">
    Hashed sharding provides a more even data distribution across the
sharded cluster at the cost of reducing
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-query-router/#std-label-sharding-query-isolation">
     Targeted Operations vs. Broadcast Operations
    </a>
    . Post-hash, documents with "close"
shard key values are unlikely to be on the same chunk or shard - the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
     <code class="leafygreen-ui-1l06pbn">
      mongos
     </code>
    </a>
    is more likely to perform
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-query-router/#std-label-sharding-mongos-broadcast">
     Broadcast Operations
    </a>
    to fulfill a given ranged query.
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
     <code class="leafygreen-ui-1l06pbn">
      mongos
     </code>
    </a>
    can target queries with equality matches to a
single shard.
   </p>
   <div class="figure css-5vkxs0" style="width:700px">
    <img alt="Diagram of the hashed based segmentation." class="leafygreen-ui-1qnf57e" height="250" src="/docs/manual/images/sharding-hash-based.bakedsvg.svg" style="--border-color:#C1C7C6" width="700"/>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    Hashed indexes compute the hash value of a single field as the index
value; this value is used as your shard key.
    <a class="footnote-reference header-buffer" href="#footnote-hashvalue" id="ref-hashvalue-id1">
     [
     <!-- -->
     1
     <!-- -->
     ]
    </a>
   </p>
  </dd>
  <dt>
   Sharding on a Compound Hashed Index
  </dt>
  <dd class="css-1ufxuu7">
   <p class="leafygreen-ui-1kp3ins">
    MongoDB includes support for creating compound indexes with a single
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-type-hashed">
     hashed field
    </a>
    . To create a compound hashed
index, specify
    <code class="leafygreen-ui-1l06pbn">
     hashed
    </code>
    as the value of any single index key when
creating the index.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Compound hashed index compute the hash value of a single field in the
compound index; this value is used along with the other fields in the
index as your shard key.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Compound hashed sharding supports features like
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
     zone sharding
    </a>
    , where the prefix (i.e. first) non-hashed field or
fields support zone ranges while the hashed field supports more even
distribution of the sharded data. Compound hashed sharding also
supports shard keys with a hashed prefix for resolving data
distribution issues related to
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-shard-key-monotonic">
     monotonically increasing
fields
     <!-- -->
     .
    </a>
   </p>
  </dd>
 </dl>
 <div class="leafygreen-ui-13vw3ie" role="note">
  <h2 class="leafygreen-ui-1fn4p2b">
   Tip
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    MongoDB automatically computes the hashes when resolving queries using
hashed indexes.  Applications do
    <strong>
     not
    </strong>
    need to compute hashes.
   </p>
  </div>
 </div>
 <div class="leafygreen-ui-1sjx7ru" role="note">
  <h2 class="leafygreen-ui-29m78u">
   Warning
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    MongoDB
    <code class="leafygreen-ui-1l06pbn">
     hashed
    </code>
    indexes truncate floating point numbers to 64-bit integers
before hashing. For example, a
    <code class="leafygreen-ui-1l06pbn">
     hashed
    </code>
    index would store the same
value for a field that held a value of
    <code class="leafygreen-ui-1l06pbn">
     2.3
    </code>
    ,
    <code class="leafygreen-ui-1l06pbn">
     2.2
    </code>
    , and
    <code class="leafygreen-ui-1l06pbn">
     2.9
    </code>
    .
To prevent collisions, do not use a
    <code class="leafygreen-ui-1l06pbn">
     hashed
    </code>
    index for floating
point numbers that cannot be reliably converted to 64-bit
integers (and then back to floating point). MongoDB
    <code class="leafygreen-ui-1l06pbn">
     hashed
    </code>
    indexes do
not support floating point values larger than 2
    <sup>
     53
    </sup>
    .
   </p>
   <p class="leafygreen-ui-1kp3ins">
    To see what the hashed value would be for a key, see
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/convertShardKeyToHashed/#mongodb-method-convertShardKeyToHashed">
     <code class="leafygreen-ui-1l06pbn">
      convertShardKeyToHashed()
     </code>
     .
    </a>
   </p>
  </div>
 </div>
 <table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-hashvalue" rules="none">
  <colgroup>
   <col/>
  </colgroup>
  <tbody valign="top">
   <tr>
    <td class="leafygreen-ui-17r2xs0">
     [
     <a href="#ref-hashvalue-id1">
      1
     </a>
     ]
    </td>
    <td class="leafygreen-ui-17r2xs0">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        mongosh
       </code>
      </span>
     </a>
     provides the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/convertShardKeyToHashed/#mongodb-method-convertShardKeyToHashed">
      <code class="leafygreen-ui-1l06pbn">
       convertShardKeyToHashed()
      </code>
     </a>
     method. This method uses the same hashing function as the hashed index and
can be used to see what the hashed value would be for a key.
    </td>
   </tr>
  </tbody>
 </table>
 <span class="leafygreen-ui-1qnjd6d" id="std-label-hashed-sharding-shard-key">
 </span>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Hashed Sharding Shard Key
   <a class="headerlink leafygreen-ui-1kepx47" href="#hashed-sharding-shard-key" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="hashed-sharding-shard-key">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The field you choose as your hashed shard key should have a good
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-shard-key-range">
    cardinality
   </a>
   , or large number of different values.
Hashed keys are ideal for shard keys with fields that change
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-shard-key-monotonic">
    monotonically
   </a>
   like
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-ObjectId">
    ObjectId
   </a>
   values or
timestamps. A good example of this is the default
   <code class="leafygreen-ui-1l06pbn">
    _id
   </code>
   field, assuming
it only contains
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-ObjectId">
    ObjectId
   </a>
   values.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   To shard a collection using a hashed shard key, see
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-shard-cluster/#std-label-deploy-hashed-sharded-cluster-shard-collection">
    Shard a Collection
    <!-- -->
    .
   </a>
  </p>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-hashed-versus-ranged-sharding">
  </span>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Hashed vs Ranged Sharding
   <a class="headerlink leafygreen-ui-1kepx47" href="#hashed-vs-ranged-sharding" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="hashed-vs-ranged-sharding">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Given a collection using a monotonically increasing value
   <code class="leafygreen-ui-1l06pbn">
    X
   </code>
   as the
shard key, using ranged sharding results in a distribution of incoming
inserts similar to the following:
  </p>
  <div class="css-y1v7l ej97su30">
   <div role="button" tabindex="-1">
    <img alt="Diagram of poor shard key distribution due to monotonically increasing or decreasing shard key" class="leafygreen-ui-1qnf57e" height="600" src="/docs/manual/images/sharded-cluster-monotonic-distribution.bakedsvg.svg" style="--border-color:#C1C7C6" width="1200"/>
    <div class="css-19pi7x2 ej97su31" style="--color:#444">
     click to enlarge
    </div>
   </div>
  </div>
  <p class="leafygreen-ui-1kp3ins">
   Since the value of
   <code class="leafygreen-ui-1l06pbn">
    X
   </code>
   is always increasing, the chunk with an upper bound
of
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MaxKey">
    <code class="leafygreen-ui-1l06pbn">
     MaxKey
    </code>
   </a>
   receives the majority incoming writes. This
restricts insert operations to the single shard containing this chunk, which
reduces or removes the advantage of distributed writes in a sharded cluster.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   By using a hashed index on
   <code class="leafygreen-ui-1l06pbn">
    X
   </code>
   , the distribution of inserts is similar
to the following:
  </p>
  <div class="css-y1v7l ej97su30">
   <div role="button" tabindex="-1">
    <img alt="Diagram of hashed shard key distribution" class="leafygreen-ui-1qnf57e" height="552" src="/docs/manual/images/sharded-cluster-hashed-distribution.bakedsvg.svg" style="--border-color:#C1C7C6" width="1162"/>
    <div class="css-19pi7x2 ej97su31" style="--color:#444">
     click to enlarge
    </div>
   </div>
  </div>
  <p class="leafygreen-ui-1kp3ins">
   Since the data is now distributed more evenly, inserts are efficiently
distributed throughout the cluster.
  </p>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Shard the Collection
   <a class="headerlink leafygreen-ui-1kepx47" href="#shard-the-collection" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="shard-the-collection">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Use the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#mongodb-method-sh.shardCollection">
    <code class="leafygreen-ui-1l06pbn">
     sh.shardCollection()
    </code>
   </a>
   method, specifying the full namespace
of the collection and the target
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-type-hashed">
    hashed index
   </a>
   to use as the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
    shard key
    <!-- -->
    .
   </a>
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.shardCollection( \"database.collection\", { &lt;field&gt; : \"hashed\" } )","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-1b1bvay">
     <pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">shardCollection</span>( <span class="lg-highlight-string">"database.collection"</span>, <!-- -->{ &lt;<!-- -->field<!-- -->&gt; <!-- -->: <span class="lg-highlight-string">"hashed"</span> } )</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
         </path>
        </svg>
       </div>
      </button>
     </div>
    </div>
   </div>
  </div>
  <p class="leafygreen-ui-1kp3ins">
   To shard a collection on a
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/create/#std-label-index-type-compound-hashed">
    compound hashed index
   </a>
   , specify
the full namespace of the collection and the target compound
hashed index to use as the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
    shard key
    <!-- -->
    :
   </a>
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.shardCollection(\n  \"database.collection\",\n  { \"fieldA\" : 1, \"fieldB\" : 1, \"fieldC\" : \"hashed\" }\n)","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">shardCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"database.collection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"fieldA"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"fieldB"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"fieldC"</span> <!-- -->: <span class="lg-highlight-string">"hashed"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
         </path>
        </svg>
       </div>
      </button>
     </div>
    </div>
   </div>
  </div>
  <div class="leafygreen-ui-85da11" role="note">
   <h2 class="leafygreen-ui-1ac4g5h">
    Important
   </h2>
   <div class="leafygreen-ui-9mycqc">
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       Starting in MongoDB 5.0, you can
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
        reshard a collection
       </a>
       by changing a collection's shard key.
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You can
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-refine-a-shard-key/#std-label-shard-key-refine">
        refine a shard key
       </a>
       by adding a suffix
field or fields to the existing shard key.
      </p>
     </li>
    </ul>
   </div>
  </div>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Shard a Populated Collection
    <a class="headerlink leafygreen-ui-1kepx47" href="#shard-a-populated-collection" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="shard-a-populated-collection">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    If you shard a populated collection using a hashed shard key:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The sharding operation creates an initial chunk to cover all of the
shard key values.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      After the initial chunk creation, the balancer moves ranges of the
initial chunk when it needs to balance data.
     </p>
    </li>
   </ul>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-shard-empty-collection">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Shard an Empty Collection
    <a class="headerlink leafygreen-ui-1kepx47" href="#shard-an-empty-collection" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="shard-an-empty-collection">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The shard collection operation can perform an initial chunk creation and
distribution for empty or non-existing collections if
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/">
     zones and zone
ranges
    </a>
    have been defined for the collection. Initial
creation and distribution of chunk allows for faster setup of zoned
sharding. After the initial distribution, the balancer manages the
chunk distribution going forward per usual.
   </p>
   <dl>
    <dt>
     Sharding Empty Collection on Single Field Hashed Shard Key
    </dt>
    <dd class="css-1ufxuu7">
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With no
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zones-sharding">
         zones and zone ranges
        </a>
        specified
for the empty or non-existing collection:
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The sharding operation creates an empty chunk to cover the entire
range of the shard key values. Starting in version 7.2, the
operation creates 1 chunk per shard by default and migrates
across the cluster. You can use
          <code class="leafygreen-ui-1l06pbn">
           numInitialChunks
          </code>
          option to
specify a different number of initial chunks and cause an initial
chunk distribution. This initial creation and distribution of
chunks allows for faster setup of sharding.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          After the initial distribution, the balancer manages the chunk
distribution going forward.
         </p>
        </li>
       </ul>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With zones and zone ranges specified
for the empty or a non-existing collection:
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The sharding operation creates empty chunks for the defined zone
ranges as well as any additional chunks to cover the entire range
of the shard key values and performs an initial chunk distribution
based on the zone ranges. This initial creation and distribution
of chunks allows for faster setup of zoned sharding.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          After the initial distribution, the balancer manages the chunk
distribution going forward.
         </p>
        </li>
       </ul>
      </li>
     </ul>
    </dd>
    <dt>
     Sharding Empty Collection on Compound Hashed Shard Key with Hashed Field Prefix
    </dt>
    <dd class="css-1ufxuu7">
     <p class="leafygreen-ui-1kp3ins">
      If the compound hashed shard key has the hashed field as the prefix
(the hashed field is the first field in the shard key):
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With no zones and zone ranges specified
for the empty or non-existing collection:
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The sharding operation creates empty chunks to cover the entire
range of the shard key values and performs an initial chunk
distribution. The value of all non-hashed fields is
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MinKey">
           <code class="leafygreen-ui-1l06pbn">
            MinKey
           </code>
          </a>
          at
each split point. Starting in version 7.2, the operation creates
1 chunk per shard by default and migrates across the cluster. You
can use
          <code class="leafygreen-ui-1l06pbn">
           numInitialChunks
          </code>
          option to specify a different number
of initial chunks and cause an initial chunk distribution. This
initial creation and distribution of chunks allows for faster
setup of sharding.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          After the initial distribution, the balancer manages the chunk
distribution going forward.
         </p>
        </li>
       </ul>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With a
        <em>
         single
        </em>
        zone with a
range from
        <code class="leafygreen-ui-1l06pbn">
         MinKey
        </code>
        to
        <code class="leafygreen-ui-1l06pbn">
         MaxKey
        </code>
        specified
for the empty or a non-existing collection
        <em>
         and
        </em>
        the
        <code class="leafygreen-ui-1l06pbn">
         presplitHashedZones
        </code>
        option specified to
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#mongodb-method-sh.shardCollection">
         <code class="leafygreen-ui-1l06pbn">
          sh.shardCollection()
         </code>
         :
        </a>
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The sharding operation creates empty chunks for the defined zone
range as well as any additional chunks to cover the entire range
of the shard key values and performs an initial chunk
distribution based on the zone ranges. This initial creation and
distribution of chunks allows for faster setup of zoned sharding.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          After the initial distribution, the balancer manages the chunk
distribution going forward.
         </p>
        </li>
       </ul>
      </li>
     </ul>
    </dd>
    <dt>
     Sharding Empty Collection on Compound Hashed Shard Key with Non-Hashed Prefix
    </dt>
    <dd class="css-1ufxuu7">
     <p class="leafygreen-ui-1kp3ins">
      If the compound hashed shard key has one or more non-hashed fields as
the prefix (i.e. the hashed field is
      <em>
       not
      </em>
      the first field in the
shard key):
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With no zones and zone ranges specified
for the empty or non-existing collection
        <em>
         and
        </em>
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#std-label-method-shard-collection-presplitHashedZones">
         preSplitHashedZones
        </a>
        is
        <code class="leafygreen-ui-1l06pbn">
         false
        </code>
        or
omitted, MongoDB does not perform any initial chunk creation or
distribution when sharding the collection.
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With no zones and zone ranges specified
for the empty or non-existing collection
        <em>
         and
        </em>
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#std-label-method-shard-collection-presplitHashedZones">
         preSplitHashedZones
        </a>
        ,
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#mongodb-method-sh.shardCollection">
         <code class="leafygreen-ui-1l06pbn">
          sh.shardCollection()
         </code>
        </a>
        /
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
         <code class="leafygreen-ui-1l06pbn">
          shardCollection
         </code>
        </a>
        returns an error.
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        With zones and zone ranges specified
for the empty or a non-existing collection
        <em>
         and
        </em>
        the
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#std-label-method-shard-collection-presplitHashedZones">
         preSplitHashedZones
        </a>
        option specified to
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#mongodb-method-sh.shardCollection">
         <code class="leafygreen-ui-1l06pbn">
          sh.shardCollection()
         </code>
         :
        </a>
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The sharding operation creates empty chunks for the defined zone
ranges as well as any additional chunks to cover the entire range
of the shard key values.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The sharding operation further subdivides the initial chunk for
each range, such that each shard in the zone is allocated an
equal number of chunks.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          This initial creation and distribution of chunks allows for
faster setup of zoned sharding. After the initial distribution,
the balancer manages the chunk distribution going forward.
         </p>
        </li>
       </ul>
       <p class="leafygreen-ui-1kp3ins">
        The defined ranges for each zone
        <em>
         must
        </em>
        meet certain requirements.
For a description of the requirements and a complete example, see
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.updateZoneKeyRange/#std-label-pre-define-zone-range-hashed-example">
         Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection
         <!-- -->
         .
        </a>
       </p>
      </li>
     </ul>
    </dd>
   </dl>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Drop a Hashed Shard Key Index
    <a class="headerlink leafygreen-ui-1kepx47" href="#drop-a-hashed-shard-key-index" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="drop-a-hashed-shard-key-index">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Starting in MongoDB 7.0.3 (and 6.0.12 and 5.0.22), you can drop the
index for a hashed shard key.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    This can speed up data insertion for collections sharded with a hashed
shard key. It can also speed up data ingestion when using
    <code class="leafygreen-ui-1l06pbn">
     mongosync
    </code>
    .
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For details, see
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/drop-a-hashed-shard-key-index/#std-label-drop-a-hashed-shard-key-index">
     Drop a Hashed Shard Key Index
     <!-- -->
     .
    </a>
   </p>
   <div class="leafygreen-ui-13vw3ie" role="note">
    <h2 class="leafygreen-ui-1fn4p2b">
     Tip
    </h2>
    <h3 class="leafygreen-ui-144d5kf">
     <strong>
      See also:
     </strong>
    </h3>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      To learn how to deploy a sharded cluster and implement hashed
sharding, see
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-shard-cluster/#std-label-sharding-procedure-setup">
       Deploy a Self-Managed Sharded Cluster
       <!-- -->
       .
      </a>
     </p>
    </div>
   </div>
  </section>
 </section>
</section>
