<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Shard Keys
  <a class="headerlink leafygreen-ui-1kepx47" href="#shard-keys" 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-keys">
   </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="#shard-key-indexes" target="_self">
     <span>
      Shard Key Indexes
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#missing-shard-key-fields" target="_self">
     <span>
      Missing Shard Key Fields
     </span>
    </a>
   </li>
  </ul>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  The shard key is either a single indexed
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-field">
   field
  </a>
  or multiple
fields covered by a
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-compound-index">
   compound index
  </a>
  that determines the
distribution of the collection's
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-document">
   documents
  </a>
  among the
cluster's
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
   shards
   <!-- -->
   .
  </a>
 </p>
 <p class="leafygreen-ui-1kp3ins">
  MongoDB divides the span of shard key values (or hashed
shard key values) into non-overlapping ranges of shard key values (or
hashed shard key values). Each range is associated with a
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
   chunk
  </a>
  , and MongoDB attempts to distribute chunks evenly among
the shards in the cluster.
 </p>
 <div class="figure css-5vkxs0" style="width:700px">
  <img alt="Diagram of the shard key value space segmented into smaller ranges or chunks." class="leafygreen-ui-1qnf57e" height="250" src="/docs/manual/images/sharding-range-based.bakedsvg.svg" style="--border-color:#C1C7C6" width="760"/>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  The shard key has a direct relationship to the effectiveness of chunk
distribution. See
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-sharding-shard-key-selection">
   Choose a Shard Key
   <!-- -->
   .
  </a>
 </p>
 <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-internals-shard-key-indexes">
 </span>
 <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-shard-key-indexes">
 </span>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Shard Key Indexes
   <a class="headerlink leafygreen-ui-1kepx47" href="#shard-key-indexes" 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-indexes">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   All sharded collections
   <strong>
    must
   </strong>
   have an index that supports the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
    shard key
   </a>
   . The index can be an index on the shard key or a
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-compound-index">
    compound index
   </a>
   where the shard key is a
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-compound-index-prefix">
    prefix
   </a>
   of the index.
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     If the collection is empty,
     <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>
     creates
the index on the shard key if such an index does not already exists.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     If the collection is not empty, you must create the index first
before using
     <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>
   </li>
  </ul>
  <p class="leafygreen-ui-1kp3ins">
   You cannot
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndex/#std-label-collection-drop-index">
    drop
   </a>
   or
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.hideIndex/#std-label-collection-hide-index">
    hide
   </a>
   an index if it is the only
non-hidden index that supports the shard key.
  </p>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-shard-key-unique">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Unique Indexes
    <a class="headerlink leafygreen-ui-1kepx47" href="#unique-indexes" 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="unique-indexes">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    MongoDB can enforce a uniqueness constraint on a ranged shard key index.
Through the use of a unique index on the shard key, MongoDB enforces
uniqueness on the entire key combination and not individual components
of the shard key.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For a ranged sharded collection, only the following indexes can be
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
     unique
     <!-- -->
     :
    </a>
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The index on the shard key
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      A
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-compound-index">
       compound index
      </a>
      where the shard key is a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-compound-index-prefix">
       prefix
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The default
      <code class="leafygreen-ui-1l06pbn">
       _id
      </code>
      index.
     </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">
        Sharded clusters only enforce the uniqueness constraint on
        <code class="leafygreen-ui-1l06pbn">
         _id
        </code>
        fields across the cluster when the
        <code class="leafygreen-ui-1l06pbn">
         _id
        </code>
        field is
also the shard key.
       </p>
       <p class="leafygreen-ui-1kp3ins">
        If the
        <code class="leafygreen-ui-1l06pbn">
         _id
        </code>
        field is not the shard key or if it is only
the prefix to the shard key, the uniqueness constraint
applies only to the shard that stores the document. This
means that two or more documents can have the same
        <code class="leafygreen-ui-1l06pbn">
         _id
        </code>
        value, provided they occur on different shards.
       </p>
       <p class="leafygreen-ui-1kp3ins">
        In cases where the
        <code class="leafygreen-ui-1l06pbn">
         _id
        </code>
        field is not the shard key,
MongoDB expects applications to enforce the uniqueness of
        <code class="leafygreen-ui-1l06pbn">
         _id
        </code>
        values across the shards.
       </p>
      </div>
     </div>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The unique index constraints mean that:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For a to-be-sharded collection, you cannot shard the collection if
the collection has other unique indexes.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For an already-sharded collection, you cannot create unique indexes
on other fields.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      A unique index stores a null value for a document missing the
indexed field; that is a missing index field is treated as another
instance of a
      <code class="leafygreen-ui-1l06pbn">
       null
      </code>
      index key value. For more information, see
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-unique-index-and-missing-field">
       Missing Document Field in a Unique Single-Field Index
       <!-- -->
       .
      </a>
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    To enforce uniqueness on the shard key values, pass the
    <code class="leafygreen-ui-1l06pbn">
     unique
    </code>
    parameter as
    <code class="leafygreen-ui-1l06pbn">
     true
    </code>
    to 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:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If the collection is empty,
      <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>
      creates the unique index on the
shard key if such an index does not already exist.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If the collection is not empty, you must create the index first
before using
      <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>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Although you can have a unique
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-compound-index">
     compound index
    </a>
    where the shard
key is a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-compound-index-prefix">
     prefix
    </a>
    , if using
    <code class="leafygreen-ui-1l06pbn">
     unique
    </code>
    parameter, the collection must have a unique index that is on the shard
key.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    You cannot specify a unique constraint on a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-type-hashed">
     hashed index
     <!-- -->
     .
    </a>
   </p>
   <p class="leafygreen-ui-1kp3ins">
    To maintain uniqueness on a field that is not your shard key,
see
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/unique-constraints-on-arbitrary-fields/#std-label-shard-key-arbitrary-uniqueness">
     Unique Constraints on Arbitrary Fields
     <!-- -->
     .
    </a>
   </p>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-shard-key-missing">
   </span>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Missing Shard Key Fields
   <a class="headerlink leafygreen-ui-1kepx47" href="#missing-shard-key-fields" 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="missing-shard-key-fields">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Documents in sharded collections can be missing the shard key fields.
To set missing shard key fields, see
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-set-missing-shard-key-fields/#std-label-shard-key-missing-set">
    Set Missing Shard Key Fields
    <!-- -->
    .
   </a>
  </p>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Chunk Range and Missing Shard Key Fields
    <a class="headerlink leafygreen-ui-1kepx47" href="#chunk-range-and-missing-shard-key-fields" 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="chunk-range-and-missing-shard-key-fields">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Missing shard key fields fall within the same chunk range as shard keys
with null values. For example, if the shard key is on the fields
    <code class="leafygreen-ui-1l06pbn">
     { x:
1, y: 1 }
    </code>
    , then:
   </p>
   <div class="leafygreen-ui-skkc39" tabindex="0">
    <table class="leafygreen-ui-hr8lsv">
     <colgroup>
      <col style="width:60%"/>
      <col style="width:60%"/>
     </colgroup>
     <thead class="leafygreen-ui-a9ho40">
      <tr data-testid="leafygreen-ui-header-row">
       <th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Document Missing Shard Key
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Falls into Same Range As
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           { x: "hello" }
          </code>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           { x: "hello", y: null }
          </code>
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           { y: "goodbye" }
          </code>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           { x: null, y: "goodbye" }
          </code>
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           { z: "oops" }
          </code>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           { x: null, y: null }
          </code>
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-shard-key-missing-read-write-ops">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Read/Write Operations and Missing Shard Key Fields
    <a class="headerlink leafygreen-ui-1kepx47" href="#read-write-operations-and-missing-shard-key-fields" 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="read-write-operations-and-missing-shard-key-fields">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    To target documents with missing shard key fields, you can use the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/exists/#mongodb-query-op.-exists">
     <code class="leafygreen-ui-1l06pbn">
      { $exists: false }
     </code>
    </a>
    filter condition on the shard key
fields. For example, if the shard key is on the fields
    <code class="leafygreen-ui-1l06pbn">
     { x: 1, y: 1
}
    </code>
    , you can find the documents with missing shard key fields by running
this query:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.shardedcollection.find( { $or: [ { x: { $exists: false } }, { y: { $exists: false } } ] } )","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">db.<span class="lg-highlight-property">shardedcollection</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$or</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">x</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</span>: <span class="lg-highlight-literal">false</span> } }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">y</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</span>: <span class="lg-highlight-literal">false</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">
    If you specify a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-for-null-fields/">
     null equality match
    </a>
    filter condition (e.g.
    <code class="leafygreen-ui-1l06pbn">
     { x: null
}
    </code>
    ), the filter matches
    <strong class="css-4dei7l">
     both
    </strong>
    those documents with missing shard
key fields
    <strong class="css-4dei7l">
     and
    </strong>
    those with shard key fields set to
    <code class="leafygreen-ui-1l06pbn">
     null
    </code>
    .
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Some write operations, such as a write with an
    <code class="leafygreen-ui-1l06pbn">
     upsert
    </code>
    specification, require an equality match on the shard key. In those
cases, to target a document that is missing the shard key, include
another filter condition in addition to the
    <code class="leafygreen-ui-1l06pbn">
     null
    </code>
    equality match.
For example:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: &lt;value&gt;, &lt;shardkeyfield&gt;: null } // _id of the document missing shard key","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">{ <span class="lg-highlight-attr">_id</span>: &lt;<!-- -->value<!-- -->&gt;<!-- -->,<!-- --> <!-- -->&lt;<!-- -->shardkeyfield<!-- -->&gt;<!-- -->: <span class="lg-highlight-literal">null</span> } <span class="lg-highlight-comment">// _id of</span><span class="lg-highlight-comment"> the document missing </span><span class="lg-highlight-comment">shard key</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>
