<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Distributed Local Writes for Insert Only Workloads
  <a class="headerlink leafygreen-ui-1kepx47" href="#distributed-local-writes-for-insert-only-workloads" 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="distributed-local-writes-for-insert-only-workloads">
   </div>
  </a>
 </h1>
 <div class="leafygreen-ui-a5yxub">
  <p class="css-1cmx2d4 ef7gu8j0">
   On this page
  </p>
  <ul class="css-1qb0mst ef7gu8j1">
   <li class="leafygreen-ui-1e7a0ei">
    <a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#scenario" target="_self">
     <span>
      Scenario
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
     <span>
      Procedure
     </span>
    </a>
   </li>
  </ul>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  MongoDB Tag Aware Sharding allows administrators to control data distribution
in a sharded cluster by defining ranges of the
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
   shard key
  </a>
  and tagging
them to one or more shards.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  This tutorial uses
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
   Zones
  </a>
  along with a multi-datacenter
sharded cluster deployment and application-side logic to support distributed
local writes, as well as high write availability in the event of a replica set
election or datacenter failure.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  By defining the zones and the zone ranges
  <em>
   before
  </em>
  sharding an empty
or a non-existing collection, the shard collection operation creates
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. After the initial distribution, the balancer manages the
chunk distribution going forward.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  See
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.updateZoneKeyRange/#std-label-pre-define-zone-range-example">
   Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection
  </a>
  for an example.
 </p>
 <div class="leafygreen-ui-85da11" role="note">
  <h2 class="leafygreen-ui-1ac4g5h">
   Important
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    The concepts discussed in this tutorial require a specific deployment
architecture, as well as application-level logic.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    These concepts require familiarity with MongoDB
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
     sharded clusters
    </a>
    ,
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
     replica sets
    </a>
    , and the general
behavior of
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
     zones
     <!-- -->
     .
    </a>
   </p>
   <p class="leafygreen-ui-1kp3ins">
    This tutorial assumes an insert-only or insert-intensive workload. The
concepts and strategies discussed in this tutorial are not well suited for
use cases that require fast reads or updates.
   </p>
  </div>
 </div>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Scenario
   <a class="headerlink leafygreen-ui-1kepx47" href="#scenario" 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="scenario">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Consider an insert-intensive application, where reads are infrequent and low
priority compared to writes. The application writes documents to a sharded
collection, and requires near-constant uptime from the
database to support its SLAs or SLOs.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   The following represents a partial view of the format of documents the
application writes to the database:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"_id\" : ObjectId(\"56f08c447fe58b2e96f595fa\"),\n   \"message_id\" : 329620,\n   \"datacenter\" : \"alfa\",\n   \"userid\" : 123,\n   ...\n}\n{\n   \"_id\" : ObjectId(\"56f08c447fe58b2e96f595fb\"),\n   \"message_id\" : 578494,\n   \"datacenter\" : \"bravo\",\n   \"userid\" : 456,\n   ...\n}\n{\n   \"_id\" : ObjectId(\"56f08c447fe58b2e96f595fc\"),\n   \"message_id\" : 689979,\n   \"datacenter\" : \"bravo\",\n   \"userid\" : 789,\n   ...\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c447fe58b2e96f595fa"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">329620</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"alfa"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">123</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c447fe58b2e96f595fb"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">578494</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"bravo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">456</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c447fe58b2e96f595fc"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">689979</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"bravo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">789</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->...</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>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Shard Key
    <a class="headerlink leafygreen-ui-1kepx47" href="#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="shard-key">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The collection uses the
    <code class="leafygreen-ui-1l06pbn">
     { datacenter : 1, userid : 1 }
    </code>
    compound index as
the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
     shard key
     <!-- -->
     .
    </a>
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field in each document allows for creating a tag range on
each distinct datacenter value. Without the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field, it would not
be possible to associate a document with a specific datacenter.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The
    <code class="leafygreen-ui-1l06pbn">
     userid
    </code>
    field provides a high
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-shard-key-cardinality">
     cardinality
    </a>
    and low
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-shard-key-frequency">
     frequency
    </a>
    component to the shard key
relative to
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    .
   </p>
   <p class="leafygreen-ui-1kp3ins">
    See
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-sharding-shard-key-requirements">
     Choosing a Shard Key
    </a>
    for more
general instructions on selecting a shard key.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Architecture
    <a class="headerlink leafygreen-ui-1kepx47" href="#architecture" 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="architecture">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The deployment consists of two datacenters,
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    and
    <code class="leafygreen-ui-1l06pbn">
     bravo
    </code>
    . There are
two shards,
    <code class="leafygreen-ui-1l06pbn">
     shard0000
    </code>
    and
    <code class="leafygreen-ui-1l06pbn">
     shard0001
    </code>
    . Each shard is a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
     replica set
    </a>
    with three members.
    <code class="leafygreen-ui-1l06pbn">
     shard0000
    </code>
    has two members on
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    and one
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-priority-0-member/#std-label-replica-set-secondary-only-members">
     priority 0 member
    </a>
    on
    <code class="leafygreen-ui-1l06pbn">
     bravo
    </code>
    .
    <code class="leafygreen-ui-1l06pbn">
     shard0001
    </code>
    has two members on
    <code class="leafygreen-ui-1l06pbn">
     bravo
    </code>
    and one
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-priority-0-member/#std-label-replica-set-secondary-only-members">
     priority 0 member
    </a>
    on
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    .
   </p>
   <div class="figure css-5vkxs0" style="width:700px">
    <img alt="Diagram of sharded cluster architecture for high availability" class="leafygreen-ui-1qnf57e" height="462" src="/docs/manual/images/sharded-cluster-high-availability-architecture.bakedsvg.svg" style="--border-color:#C1C7C6" width="562"/>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-high-availability-tags">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Tags
    <a class="headerlink leafygreen-ui-1kepx47" href="#tags" 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="tags">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    This application requires one tag per datacenter. Each shard has one
tag assigned to it based on the datacenter containing the majority of
its replica set members. There are two tag ranges, one for each datacenter.
   </p>
   <dl>
    <dt>
     <code class="leafygreen-ui-1l06pbn">
      alfa
     </code>
     Datacenter
    </dt>
    <dd class="css-1ufxuu7">
     <p class="leafygreen-ui-1kp3ins">
      Tag shards with a majority of members on this datacenter as
      <code class="leafygreen-ui-1l06pbn">
       alfa
      </code>
      .
     </p>
     <p class="leafygreen-ui-1kp3ins">
      Create a tag range with:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        a lower bound of
        <code class="leafygreen-ui-1l06pbn">
         { "datacenter" : "alfa", "userid" : MinKey }
        </code>
        ,
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        an upper bound of
        <code class="leafygreen-ui-1l06pbn">
         { "datacenter" : "alfa", "userid" : MaxKey }
        </code>
        , and
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        the tag
        <code class="leafygreen-ui-1l06pbn">
         alfa
        </code>
       </p>
      </li>
     </ul>
    </dd>
    <dt>
     <code class="leafygreen-ui-1l06pbn">
      bravo
     </code>
     Datacenter
    </dt>
    <dd class="css-1ufxuu7">
     <p class="leafygreen-ui-1kp3ins">
      Tag shards with a majority of members on this datacenter as
      <code class="leafygreen-ui-1l06pbn">
       bravo
      </code>
      .
     </p>
     <p class="leafygreen-ui-1kp3ins">
      Create a tag range with:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        a lower bound of
        <code class="leafygreen-ui-1l06pbn">
         { "datacenter" : "bravo", "userid" : MinKey }
        </code>
        ,
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        an upper bound of
        <code class="leafygreen-ui-1l06pbn">
         { "datacenter" : "bravo", "userid" : MaxKey }
        </code>
        , and
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        the tag
        <code class="leafygreen-ui-1l06pbn">
         bravo
        </code>
       </p>
      </li>
     </ul>
    </dd>
   </dl>
   <div class="leafygreen-ui-15mdk4" role="note">
    <h2 class="leafygreen-ui-6n7b7d">
     Note
    </h2>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MinKey">
       <code class="leafygreen-ui-1l06pbn">
        MinKey
       </code>
      </a>
      and
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MaxKey">
       <code class="leafygreen-ui-1l06pbn">
        MaxKey
       </code>
      </a>
      values are reserved special
values for comparisons
     </p>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    Based on the
configured tags and tag ranges,
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
     <code class="leafygreen-ui-1l06pbn">
      mongos
     </code>
    </a>
    routes documents with
    <code class="leafygreen-ui-1l06pbn">
     datacenter : alfa
    </code>
    to the
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    datacenter, and documents with
    <code class="leafygreen-ui-1l06pbn">
     datacenter : bravo
    </code>
    to the
    <code class="leafygreen-ui-1l06pbn">
     bravo
    </code>
    datacenter.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Write Operations
    <a class="headerlink leafygreen-ui-1kepx47" href="#write-operations" 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="write-operations">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    If an inserted or updated document matches a configured tag range, it can only
be written to a shard with the related tag.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    MongoDB can write documents that do not match a configured tag range to any
shard in the cluster.
   </p>
   <div class="leafygreen-ui-15mdk4" role="note">
    <h2 class="leafygreen-ui-6n7b7d">
     Note
    </h2>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      The behavior described above requires the cluster to be in a steady state
with no chunks violating a configured tag range. See the following section
on the
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-high-availability-writes-balancing" target="_self">
       <span>
        balancer
       </span>
      </a>
      for
more information.
     </p>
    </div>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-high-availability-writes-balancing">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Balancer
    <a class="headerlink leafygreen-ui-1kepx47" href="#balancer" 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="balancer">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
     balancer
    </a>
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-sharding-chunk-migration">
     migrates
    </a>
    the tagged chunks to the appropriate shard. Until
the migration, shards may contain chunks that violate configured tag ranges
and tags. Once balancing completes, shards should only contain chunks whose
ranges do not violate its assigned tags and tag ranges.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Adding or removing tags or tag ranges can result in chunk migrations.
Depending on the size of your data set and the number of chunks a tag range
affects, these migrations may impact cluster performance. Consider running
your
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
     balancer
    </a>
    during specific scheduled windows.
See
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-schedule-balancing-window">
     Schedule the Balancing Window
    </a>
    for a tutorial on how to set a
scheduling window.
   </p>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-high-availability-application-behavior">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Application Behavior
    <a class="headerlink leafygreen-ui-1kepx47" href="#application-behavior" 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="application-behavior">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    By default, the application writes to the nearest datacenter. If the local
datacenter is down, or if writes to that datacenter are not acknowledged
within a set time period, the application switches to the other available
datacenter by changing the value of the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field before attempting
to write the document to the database.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The application supports write timeouts. The application uses
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
     Write Concern
    </a>
    to set a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-wc-wtimeout">
     timeout
    </a>
    for each write
operation.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    If the application encounters a write or timeout error, it modifies the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field in each document and performs the write. This routes the
document to the other datacenter. If both datacenters are down, then writes
cannot succeed. See
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-high-availability-write-failure" target="_self">
     <span>
      Resolve Write Failure
      <!-- -->
      .
     </span>
    </a>
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The application periodically checks connectivity to any data
centers marked as "down". If connectivity is restored, the application can
continue performing normal write operations.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Given the switching logic, as well as any load balancers or similar mechanisms
in place to handle client traffic between datacenters, the application cannot
predict which of the two datacenters a given document was written to. To
ensure that no documents are missed as a part of read operations, the
application
    <em>
     must
    </em>
    perform
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-query-router/#std-label-sharding-mongos-broadcast">
     broadcast queries
    </a>
    by
    <em>
     not
    </em>
    including the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field as a
part of any query.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The application performs reads using a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
     read preference
    </a>
    of
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-nearest">
     <code class="leafygreen-ui-1l06pbn">
      nearest
     </code>
    </a>
    to reduce latency.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    It is possible for a write operation to succeed despite a reported timeout
error. The application responds to the error by attempting to re-write
the document to the other datacenter - this can result in a document being
duplicated across both datacenters. The application resolves duplicates
as a part of the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-high-availability-writes-read-resolution" target="_self">
     <span>
      read
     </span>
    </a>
    logic.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Switching Logic
    <a class="headerlink leafygreen-ui-1kepx47" href="#switching-logic" 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="switching-logic">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The application has logic to switch datacenters if one or more writes fail, or
if writes are not
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
     acknowledged
    </a>
    within a set time
period. The application modifies the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field based on the target
datacenter's
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-high-availability-tags" target="_self">
     <span>
      tag
     </span>
    </a>
    to direct the
document towards that datacenter.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For example, an application attempting to write to the
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    datacenter
might follow this general procedure:
   </p>
   <ol class="leafygreen-ui-rioki0" type="1">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Attempt to write document, specifying
      <code class="leafygreen-ui-1l06pbn">
       datacenter : alfa
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      On write timeout or error, log
      <code class="leafygreen-ui-1l06pbn">
       alfa
      </code>
      as momentarily down.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Attempt to write same document, modifying
      <code class="leafygreen-ui-1l06pbn">
       datacenter : bravo
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      On write timeout or error, log
      <code class="leafygreen-ui-1l06pbn">
       bravo
      </code>
      as momentarily down.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If both
      <code class="leafygreen-ui-1l06pbn">
       alfa
      </code>
      and
      <code class="leafygreen-ui-1l06pbn">
       bravo
      </code>
      are down, log and report errors.
     </p>
    </li>
   </ol>
   <p class="leafygreen-ui-1kp3ins">
    See
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-high-availability-write-failure" target="_self">
     <span>
      Resolve Write Failure
      <!-- -->
      .
     </span>
    </a>
   </p>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Procedure
   <a class="headerlink leafygreen-ui-1kepx47" href="#procedure" 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="procedure">
    </div>
   </a>
  </h2>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-high-availability-write-configure-tags">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Configure Shard Tags
    <a class="headerlink leafygreen-ui-1kepx47" href="#configure-shard-tags" 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="configure-shard-tags">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    You must be connected to a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
     <code class="leafygreen-ui-1l06pbn">
      mongos
     </code>
    </a>
    associated with the target
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
     sharded cluster
    </a>
    in order to proceed. You cannot create tags by
connecting directly to a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
     shard
    </a>
    replica set member.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Tag each shard.Tag each shard in the alfa data center with the alfa tag.Tag each shard in the bravo data center with the bravo tag.You can review the tags assigned to any given shard by running\nsh.status()."},{"@type":"HowToStep","text":"Define ranges for each tag.Define the range for the alfa database and associate it to the alfa\ntag using the sh.addTagRange() method. This method requires:The full namespace of the target collection.The inclusive lower bound of the range.The exclusive upper bound of the range.The name of the tag.Define the range for the bravo database and associate it to the\nbravo tag using the sh.addTagRange() method. This method\nrequires:The full namespace of the target collection.The inclusive lower bound of the range.The exclusive upper bound of the range.The name of the tag.The MinKey and MaxKey values are reserved special\nvalues for comparisons. MinKey always compares as less than\nevery other possible value, while MaxKey always compares as\ngreater than every other possible value. The configured ranges capture every\nuser for each datacenter."},{"@type":"HowToStep","text":"Review the changes.The next time the balancer runs, it\nmigrates data across the shards\nrespecting the configured zones.Once balancing finishes, the shards tagged as alfa should only\ncontain documents with datacenter : alfa, while shards tagged as\nbravo should only contain documents with datacenter : bravo.You can review the chunk distribution by running sh.status()."}],"name":"Configure Shard Tags","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
   </script>
   <div class="css-4nyere eccljxs0">
    <div class="css-1nuklj8 e1ckbxcu1">
     <div class="css-18fui5z e1ckbxcu0">
      <div class="css-19lsqsq e1ckbxcu2">
       1
      </div>
     </div>
     <div class="css-1ed38mk">
      <section>
       <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
        Tag each shard.
        <a class="headerlink leafygreen-ui-1kepx47" href="#tag-each-shard" 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="tag-each-shard">
         </div>
        </a>
       </h4>
       <p class="leafygreen-ui-1kp3ins">
        Tag each shard in the
        <code class="leafygreen-ui-1l06pbn">
         alfa
        </code>
        data center with the
        <code class="leafygreen-ui-1l06pbn">
         alfa
        </code>
        tag.
       </p>
       <script class="structured_data" type="application/ld+json">
        {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.addShardTag(\"shard0000\", \"alfa\")\n","programmingLanguage":"JavaScript"}
       </script>
       <div class="css-16lmvu9">
        <div class="leafygreen-ui-196mwvg">
         <div class="leafygreen-ui-yzcs8i">
          <pre class="leafygreen-ui-1q4bxgx" 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">addShardTag</span>(<span class="lg-highlight-string">"shard0000"</span>, <span class="lg-highlight-string">"alfa"</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">
        Tag each shard in the
        <code class="leafygreen-ui-1l06pbn">
         bravo
        </code>
        data center with the
        <code class="leafygreen-ui-1l06pbn">
         bravo
        </code>
        tag.
       </p>
       <script class="structured_data" type="application/ld+json">
        {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.addShardTag(\"shard0001\", \"bravo\")\n","programmingLanguage":"JavaScript"}
       </script>
       <div class="css-16lmvu9">
        <div class="leafygreen-ui-196mwvg">
         <div class="leafygreen-ui-yzcs8i">
          <pre class="leafygreen-ui-1q4bxgx" 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">addShardTag</span>(<span class="lg-highlight-string">"shard0001"</span>, <span class="lg-highlight-string">"bravo"</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">
        You can review the tags assigned to any given shard by running
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
         <code class="leafygreen-ui-1l06pbn">
          sh.status()
         </code>
         .
        </a>
       </p>
      </section>
     </div>
    </div>
    <div class="css-1nuklj8 e1ckbxcu1">
     <div class="css-18fui5z e1ckbxcu0">
      <div class="css-19lsqsq e1ckbxcu2">
       2
      </div>
     </div>
     <div class="css-1ed38mk">
      <section>
       <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
        Define ranges for each tag.
        <a class="headerlink leafygreen-ui-1kepx47" href="#define-ranges-for-each-tag" 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="define-ranges-for-each-tag">
         </div>
        </a>
       </h4>
       <p class="leafygreen-ui-1kp3ins">
        Define the range for the
        <code class="leafygreen-ui-1l06pbn">
         alfa
        </code>
        database and associate it to the
        <code class="leafygreen-ui-1l06pbn">
         alfa
        </code>
        tag using the
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.addTagRange/#mongodb-method-sh.addTagRange">
         <code class="leafygreen-ui-1l06pbn">
          sh.addTagRange()
         </code>
        </a>
        method. This method requires:
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The full namespace of the target collection.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The inclusive lower bound of the range.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The exclusive upper bound of the range.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The name of the tag.
         </p>
        </li>
       </ul>
       <script class="structured_data" type="application/ld+json">
        {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.addTagRange( \n  \"&lt;database&gt;.&lt;collection&gt;\",\n  { \"datacenter\" : \"alfa\", \"userid\" : MinKey },\n  { \"datacenter\" : \"alfa\", \"userid\" : MaxKey }, \n  \"alfa\"\n)\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">addTagRange</span>( </td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"&lt;database&gt;.&lt;collection&gt;"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"alfa"</span>, <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">MinKey</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"alfa"</span>, <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">MaxKey</span> }<!-- -->, </td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"alfa"</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>
       <p class="leafygreen-ui-1kp3ins">
        Define the range for the
        <code class="leafygreen-ui-1l06pbn">
         bravo
        </code>
        database and associate it to the
        <code class="leafygreen-ui-1l06pbn">
         bravo
        </code>
        tag using the
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.addTagRange/#mongodb-method-sh.addTagRange">
         <code class="leafygreen-ui-1l06pbn">
          sh.addTagRange()
         </code>
        </a>
        method. This method
requires:
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The full namespace of the target collection.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The inclusive lower bound of the range.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The exclusive upper bound of the range.
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          The name of the tag.
         </p>
        </li>
       </ul>
       <script class="structured_data" type="application/ld+json">
        {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.addTagRange( \n  \"&lt;database&gt;.&lt;collection&gt;\",\n  { \"datacenter\" : \"bravo\", \"userid\" : MinKey },\n  { \"datacenter\" : \"bravo\", \"userid\" : MaxKey }, \n  \"bravo\"\n)\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">addTagRange</span>( </td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"&lt;database&gt;.&lt;collection&gt;"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"bravo"</span>, <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">MinKey</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"bravo"</span>, <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">MaxKey</span> }<!-- -->, </td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"bravo"</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>
       <p class="leafygreen-ui-1kp3ins">
        The
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MinKey">
         <code class="leafygreen-ui-1l06pbn">
          MinKey
         </code>
        </a>
        and
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MaxKey">
         <code class="leafygreen-ui-1l06pbn">
          MaxKey
         </code>
        </a>
        values are reserved special
values for comparisons.
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MinKey">
         <code class="leafygreen-ui-1l06pbn">
          MinKey
         </code>
        </a>
        always compares as less than
every other possible value, while
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-MaxKey">
         <code class="leafygreen-ui-1l06pbn">
          MaxKey
         </code>
        </a>
        always compares as
greater than every other possible value. The configured ranges capture every
user for each
        <code class="leafygreen-ui-1l06pbn">
         datacenter
        </code>
        .
       </p>
      </section>
     </div>
    </div>
    <div class="css-1nuklj8 e1ckbxcu1">
     <div class="css-18fui5z e1ckbxcu0">
      <div class="css-19lsqsq e1ckbxcu2">
       3
      </div>
     </div>
     <div class="css-1ed38mk">
      <section>
       <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
        Review the changes.
        <a class="headerlink leafygreen-ui-1kepx47" href="#review-the-changes" 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="review-the-changes">
         </div>
        </a>
       </h4>
       <p class="leafygreen-ui-1kp3ins">
        The next time the
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
         balancer
        </a>
        runs, it
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-sharding-chunk-migration">
         migrates
        </a>
        data across the shards
respecting the configured zones.
       </p>
       <p class="leafygreen-ui-1kp3ins">
        Once balancing finishes, the shards tagged as
        <code class="leafygreen-ui-1l06pbn">
         alfa
        </code>
        should only
contain documents with
        <code class="leafygreen-ui-1l06pbn">
         datacenter : alfa
        </code>
        , while shards tagged as
        <code class="leafygreen-ui-1l06pbn">
         bravo
        </code>
        should only contain documents with
        <code class="leafygreen-ui-1l06pbn">
         datacenter : bravo
        </code>
        .
       </p>
       <p class="leafygreen-ui-1kp3ins">
        You can review the chunk distribution by running
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
         <code class="leafygreen-ui-1l06pbn">
          sh.status()
         </code>
         .
        </a>
       </p>
      </section>
     </div>
    </div>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-high-availability-write-failure">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Resolve Write Failure
    <a class="headerlink leafygreen-ui-1kepx47" href="#resolve-write-failure" 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="resolve-write-failure">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    When the application's default datacenter is down or inaccessible, the
application changes the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field to the other
datacenter.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For example, the application attempts to write the following document to the
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    datacenter by default:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"_id\" : ObjectId(\"56f08c447fe58b2e96f595fa\"),\n   \"message_id\" : 329620,\n   \"datacenter\" : \"alfa\",\n   \"userid\" : 123,\n   ...\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c447fe58b2e96f595fa"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">329620</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"alfa"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">123</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->...</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>
   <p class="leafygreen-ui-1kp3ins">
    If the application receives an error on attempted write, or if the write
acknowledgment takes too long, the application logs the datacenter as
unavailable and alters the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field to point to the
    <code class="leafygreen-ui-1l06pbn">
     bravo
    </code>
    datacenter.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"_id\" : ObjectId(\"56f08c457fe58b2e96f595fb\"),\n   \"message_id\" : 329620,\n   \"datacenter\" : \"bravo\",\n   \"userid\" : 123,\n   ...\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c457fe58b2e96f595fb"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">329620</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"bravo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">123</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->...</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>
   <p class="leafygreen-ui-1kp3ins">
    The application periodically checks the
    <code class="leafygreen-ui-1l06pbn">
     alfa
    </code>
    datacenter for
connectivity. If the datacenter is reachable again, the application can resume
normal writes.
   </p>
   <div class="leafygreen-ui-15mdk4" role="note">
    <h2 class="leafygreen-ui-6n7b7d">
     Note
    </h2>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      It is possible that the original write to
      <code class="leafygreen-ui-1l06pbn">
       datacenter : alfa
      </code>
      succeeded,
especially if the error was related to a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-wc-wtimeout">
       timeout
      </a>
      .
If so, the document with
      <code class="leafygreen-ui-1l06pbn">
       message_id : 329620
      </code>
      may now be duplicated
across both datacenters. Applications must resolve duplicates as a part
of
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-high-availability-writes-read-resolution" target="_self">
       <span>
        read operations
        <!-- -->
        .
       </span>
      </a>
     </p>
    </div>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-high-availability-writes-read-resolution">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Resolve Duplicate Documents on Reads
    <a class="headerlink leafygreen-ui-1kepx47" href="#resolve-duplicate-documents-on-reads" 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="resolve-duplicate-documents-on-reads">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The application's switching logic allows for potential document duplication.
When performing reads, the application resolves any duplicate documents on the
application layer.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The following query searches for documents where the
    <code class="leafygreen-ui-1l06pbn">
     userid
    </code>
    is
    <code class="leafygreen-ui-1l06pbn">
     123
    </code>
    .
Note that while
    <code class="leafygreen-ui-1l06pbn">
     userid
    </code>
    is part of the shard key, the query does not
include the
    <code class="leafygreen-ui-1l06pbn">
     datacenter
    </code>
    field, and therefore does not perform a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-query-router/#std-label-sharding-mongos-targeted">
     targeted read operation
     <!-- -->
     .
    </a>
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.find( { \"userid\" : 123 } )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-1q4bxgx" 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">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">123</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">
    The results show that the document with
    <code class="leafygreen-ui-1l06pbn">
     message_id
    </code>
    of
    <code class="leafygreen-ui-1l06pbn">
     329620
    </code>
    has been
inserted into MongoDB twice, probably as a result of a delayed write
acknowledgment.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : ObjectId(\"56f08c447fe58b2e96f595fa\"),\n  \"message_id\" : 329620\n  \"datacenter\" : \"alfa\",\n  \"userid\" : 123,\n  data : {...}\n}\n{\n  \"_id\" : ObjectId(\"56f08c457fe58b2e96f595fb\"),\n  \"message_id\" : 329620\n  \"datacenter\" : \"bravo\",\n  \"userid\" : 123,\n  ...\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c447fe58b2e96f595fa"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">329620</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"alfa"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">123</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->data <!-- -->: <!-- -->{<!-- -->...<!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56f08c457fe58b2e96f595fb"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"message_id"</span> <!-- -->: <span class="lg-highlight-number">329620</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"datacenter"</span> <!-- -->: <span class="lg-highlight-string">"bravo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"userid"</span> <!-- -->: <span class="lg-highlight-number">123</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->...</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>
   <p class="leafygreen-ui-1kp3ins">
    The application can either ignore the duplicates, taking one of the two
documents, or it can attempt to trim the duplicates until only a single
document remains.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    One method for trimming duplicates is to use the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/ObjectId.getTimestamp/#mongodb-method-ObjectId.getTimestamp">
     <code class="leafygreen-ui-1l06pbn">
      ObjectId.getTimestamp()
     </code>
    </a>
    method to extract the timestamp from the
    <code class="leafygreen-ui-1l06pbn">
     _id
    </code>
    field. The application can then keep either the first document
inserted, or the last document inserted. This assumes the
    <code class="leafygreen-ui-1l06pbn">
     _id
    </code>
    field uses the MongoDB
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/ObjectId/#mongodb-method-ObjectId">
     <code class="leafygreen-ui-1l06pbn">
      ObjectId()
     </code>
     .
    </a>
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For example, using
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/ObjectId.getTimestamp/#mongodb-method-ObjectId.getTimestamp">
     <code class="leafygreen-ui-1l06pbn">
      getTimestamp()
     </code>
    </a>
    on the document
with
    <code class="leafygreen-ui-1l06pbn">
     ObjectId("56f08c447fe58b2e96f595fa")
    </code>
    returns:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"ISODate(\"2016-03-22T00:05:24Z\")","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-1q4bxgx" 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"><span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2016-03-22T00:05:24Z"</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">
    Using
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/ObjectId.getTimestamp/#mongodb-method-ObjectId.getTimestamp">
     <code class="leafygreen-ui-1l06pbn">
      getTimestamp()
     </code>
    </a>
    on the document with
    <code class="leafygreen-ui-1l06pbn">
     ObjectId("56f08c457fe58b2e96f595fb")
    </code>
    returns:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"ISODate(\"2016-03-22T00:05:25Z\")","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-1q4bxgx" 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"><span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2016-03-22T00:05:25Z"</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>
  </section>
 </section>
</section>
