<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  findAndModify
  <a class="headerlink leafygreen-ui-1kepx47" href="#findandmodify" 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="findandmodify">
   </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="#definition" target="_self">
     <span>
      Definition
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
     <span>
      Compatibility
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
     <span>
      Syntax
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
     <span>
      Command Fields
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output" target="_self">
     <span>
      Output
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
     <span>
      Behavior
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
     <span>
      Examples
     </span>
    </a>
   </li>
  </ul>
 </div>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Definition
   <a class="headerlink leafygreen-ui-1kepx47" href="#definition" 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="definition">
    </div>
   </a>
  </h2>
  <dl class="dbcommand">
   <dt>
    <code class="leafygreen-ui-1l06pbn">
     findAndModify
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.findAndModify" title="Permalink to this definition">
     <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="mongodb-dbcommand-dbcmd.findAndModify">
     </div>
    </a>
   </dt>
   <dd>
    <p class="leafygreen-ui-1kp3ins">
     The
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        findAndModify
       </code>
      </span>
     </a>
     command updates and returns a single
document. By default, the returned document does not include the
modifications made on the update. To return the document with the
modifications made on the update, use the
     <code class="leafygreen-ui-1l06pbn">
      new
     </code>
     option.
    </p>
    <div class="leafygreen-ui-13vw3ie" role="note">
     <h2 class="leafygreen-ui-1fn4p2b">
      Tip
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       In
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
        <span>
         <code class="leafygreen-ui-1l06pbn">
          mongosh
         </code>
        </span>
       </a>
       , this command can also be run through the
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
        <code class="leafygreen-ui-1l06pbn">
         db.collection.findAndModify()
        </code>
       </a>
       helper method
       <!-- -->
       .
      </p>
      <p class="leafygreen-ui-1kp3ins">
       Helper methods are convenient for
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
        <span>
         <code class="leafygreen-ui-1l06pbn">
          mongosh
         </code>
        </span>
       </a>
       users, but they may not
return the same level of information as database commands.  In cases where
the convenience is not needed or the additional return fields are required,
use the database command.
      </p>
     </div>
    </div>
   </dd>
  </dl>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Compatibility
   <a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" 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="compatibility">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   This command is available in deployments hosted in the following environments:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
      <span>
       MongoDB Atlas
      </span>
     </a>
     : The fully
managed service for MongoDB deployments in the cloud
    </p>
   </li>
  </ul>
  <div class="leafygreen-ui-15mdk4" role="note">
   <h2 class="leafygreen-ui-6n7b7d">
    Note
   </h2>
   <div class="leafygreen-ui-9mycqc">
    <p class="leafygreen-ui-1kp3ins">
     This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
      <span>
       Unsupported Commands
       <!-- -->
       .
      </span>
     </a>
    </p>
   </div>
  </div>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
      MongoDB Enterprise
     </a>
     : The
subscription-based, self-managed version of MongoDB
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
      MongoDB Community
     </a>
     : The
source-available, free-to-use, and self-managed version of MongoDB
    </p>
   </li>
  </ul>
  <div>
   <p>
    <em>
     Changed
     <!-- -->
     in version
     <!-- -->
     5.0
    </em>
    .
   </p>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Syntax
   <a class="headerlink leafygreen-ui-1kepx47" href="#syntax" 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="syntax">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The command has the following syntax:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: &lt;collection-name&gt;,\n     query: &lt;document&gt;,\n     sort: &lt;document&gt;,\n     remove: &lt;boolean&gt;,\n     update: &lt;document&gt;,\n     new: &lt;boolean&gt;,\n     fields: &lt;document&gt;,\n     upsert: &lt;boolean&gt;,\n     bypassDocumentValidation: &lt;boolean&gt;,\n     writeConcern: &lt;document&gt;,\n     maxTimeMS: &lt;integer&gt;,\n     collation: &lt;document&gt;,\n     arrayFilters: &lt;array&gt;,\n     hint: &lt;document|string&gt;,\n     comment: &lt;any&gt;,\n     let: &lt;document&gt; // Added in MongoDB 5.0\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: &lt;<!-- -->collection<!-- -->-<!-- -->name<!-- -->&gt;<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: &lt;<span class="lg-highlight-variable lg-highlight-language">document</span>&gt;<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">sort</span>: &lt;<span class="lg-highlight-variable lg-highlight-language">document</span>&gt;<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">remove</span>: &lt;<!-- -->boolean<!-- -->&gt;<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->&lt;document or aggregation pipeline&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     new: &lt;boolean&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     fields: &lt;document&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     upsert: &lt;boolean&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     bypassDocumentValidation: &lt;boolean&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     writeConcern: &lt;document&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     maxTimeMS: &lt;integer&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     collation: &lt;document&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     arrayFilters: &lt;array&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     hint: &lt;document|string&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     comment: &lt;any&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     let: &lt;document&gt; // Added in MongoDB 5.0</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>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Command Fields
   <a class="headerlink leafygreen-ui-1kepx47" href="#command-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="command-fields">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The command takes the following fields:
  </p>
  <div class="leafygreen-ui-skkc39" tabindex="0">
   <table class="leafygreen-ui-hr8lsv">
    <colgroup>
     <col style="width:20%"/>
     <col style="width:20%"/>
     <col style="width:80%"/>
    </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>
         Field
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         Type
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         Description
        </div>
       </div>
      </th>
     </tr>
    </thead>
    <tbody>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          query
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. The selection criteria for the modification. The
          <code class="leafygreen-ui-1l06pbn">
           query
          </code>
          field
employs the same
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
           query selectors
          </a>
          as used in
the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
           <code class="leafygreen-ui-1l06pbn">
            db.collection.find()
           </code>
          </a>
          method. Although the query may
match multiple documents,
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             findAndModify
            </code>
           </span>
          </a>
          <strong>
           will only select one document to update
          </strong>
          .
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If unspecified, defaults to an empty document.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If the query argument is not a document, the operation errors.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <span class="leafygreen-ui-1qnjd6d" id="std-label-findandmodify-command-consistent-sorting">
         </span>
         <p class="leafygreen-ui-1kp3ins">
          <code class="leafygreen-ui-1l06pbn">
           sort
          </code>
         </p>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. Determines which document the operation updates if the query
selects multiple documents.
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             findAndModify
            </code>
           </span>
          </a>
          updates the first document in the
sort order specified by this argument.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If the sort argument is not a document, the operation errors.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          MongoDB does not store documents in a collection in a particular order.
When sorting on a field which contains duplicate values, documents
containing those values may be returned in any order.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If consistent sort order is desired, include at least one field in your
sort that contains unique values. The easiest way to guarantee this is
to include the
          <code class="leafygreen-ui-1l06pbn">
           _id
          </code>
          field in your sort query.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#std-label-sort-cursor-consistent-sorting">
           Sort Consistency
          </a>
          for more information.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          remove
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         boolean
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Must specify either the
         <code class="leafygreen-ui-1l06pbn">
          remove
         </code>
         or the
         <code class="leafygreen-ui-1l06pbn">
          update
         </code>
         field. Removes
the document specified in the
         <code class="leafygreen-ui-1l06pbn">
          query
         </code>
         field. Set this to
         <code class="leafygreen-ui-1l06pbn">
          true
         </code>
         to remove the selected document . The default is
         <code class="leafygreen-ui-1l06pbn">
          false
         </code>
         .
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          update
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document or array
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Must specify either the
          <code class="leafygreen-ui-1l06pbn">
           remove
          </code>
          or the
          <code class="leafygreen-ui-1l06pbn">
           update
          </code>
          field. Performs
an update of the selected document.
         </p>
         <ul class="leafygreen-ui-rioki0">
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            If passed a document with
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators">
             update operator expressions
            </a>
            ,
            <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
             <span>
              <code class="leafygreen-ui-1l06pbn">
               findAndModify
              </code>
             </span>
            </a>
            performs the specified
modification.
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            If passed a replacement document
            <code class="leafygreen-ui-1l06pbn">
             { &lt;field1&gt;: &lt;value1&gt;, ...}
            </code>
            ,
the
            <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
             <span>
              <code class="leafygreen-ui-1l06pbn">
               findAndModify
              </code>
             </span>
            </a>
            performs a replacement.
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            If passed an
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
             aggregation pipeline
            </a>
            <code class="leafygreen-ui-1l06pbn">
             [ &lt;stage1&gt;, &lt;stage2&gt;, ... ]
            </code>
            ,
            <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
             <span>
              <code class="leafygreen-ui-1l06pbn">
               findAndModify
              </code>
             </span>
            </a>
            updates the document per
the pipeline. The pipeline can consist of the following stages:
           </p>
           <ul class="leafygreen-ui-rioki0">
            <li class="leafygreen-ui-d7p7b6">
             <p class="leafygreen-ui-1kp3ins">
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
               <code class="leafygreen-ui-1l06pbn">
                $addFields
               </code>
              </a>
              and its alias
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
               <code class="leafygreen-ui-1l06pbn">
                $set
               </code>
              </a>
             </p>
            </li>
            <li class="leafygreen-ui-d7p7b6">
             <p class="leafygreen-ui-1kp3ins">
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
               <code class="leafygreen-ui-1l06pbn">
                $project
               </code>
              </a>
              and its alias
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
               <code class="leafygreen-ui-1l06pbn">
                $unset
               </code>
              </a>
             </p>
            </li>
            <li class="leafygreen-ui-d7p7b6">
             <p class="leafygreen-ui-1kp3ins">
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
               <code class="leafygreen-ui-1l06pbn">
                $replaceRoot
               </code>
              </a>
              and its alias
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
               <code class="leafygreen-ui-1l06pbn">
                $replaceWith
               </code>
               .
              </a>
             </p>
            </li>
           </ul>
          </li>
         </ul>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          new
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         boolean
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Optional. When
         <code class="leafygreen-ui-1l06pbn">
          true
         </code>
         , returns the updated document rather than the original.
The default is
         <code class="leafygreen-ui-1l06pbn">
          false
         </code>
         .
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          fields
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. A subset of fields to return. The
          <code class="leafygreen-ui-1l06pbn">
           fields
          </code>
          document specifies an
inclusion of a field with
          <code class="leafygreen-ui-1l06pbn">
           1
          </code>
          , as in:
          <code class="leafygreen-ui-1l06pbn">
           fields: { &lt;field1&gt;: 1,
&lt;field2&gt;: 1, ... }
          </code>
          . See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#std-label-find-projection">
           Projection
           <!-- -->
           .
          </a>
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If the fields argument is not a document, the operation errors.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          upsert
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         boolean
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. Used in conjunction with the
          <code class="leafygreen-ui-1l06pbn">
           update
          </code>
          field.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          When
          <code class="leafygreen-ui-1l06pbn">
           true
          </code>
          ,
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             findAndModify
            </code>
           </span>
          </a>
          either:
         </p>
         <ul class="leafygreen-ui-rioki0">
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            Creates a new document if no documents match the
            <code class="leafygreen-ui-1l06pbn">
             query
            </code>
            .
For more details see
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.update/#std-label-upsert-behavior">
             upsert behavior
             <!-- -->
             .
            </a>
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            Updates a single document that matches the
            <code class="leafygreen-ui-1l06pbn">
             query
            </code>
            .
           </p>
          </li>
         </ul>
         <p class="leafygreen-ui-1kp3ins">
          To avoid multiple
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-upsert">
           upserts
          </a>
          , ensure that the
          <code class="leafygreen-ui-1l06pbn">
           query
          </code>
          field(s) are
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
           uniquely indexed
          </a>
          . See
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-upsert-and-unique-index-dbcommand" target="_self">
           <span>
            Upsert with Unique Index
           </span>
          </a>
          for an example.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Defaults to
          <code class="leafygreen-ui-1l06pbn">
           false
          </code>
          , which does
          <em>
           not
          </em>
          insert a new document when no
match is found.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          bypassDocumentValidation
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         boolean
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Optional. Enables
         <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
          <span>
           <code class="leafygreen-ui-1l06pbn">
            findAndModify
           </code>
          </span>
         </a>
         to bypass document validation
during the operation. This lets you update documents that do not
meet the validation requirements.
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          writeConcern
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. A document expressing the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
           write concern
          </a>
          .
Omit to use the default write concern.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Do not explicitly set the write concern for the operation if run in
a transaction. To use write concern with transactions, see
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-write-concern">
           Transactions and Write Concern
           <!-- -->
           .
          </a>
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          maxTimeMS
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         non-negative integer
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Specifies a time limit in milliseconds.
If you do not specify a value for
          <code class="leafygreen-ui-1l06pbn">
           maxTimeMS
          </code>
          , operations will not time out.
A value of
          <code class="leafygreen-ui-1l06pbn">
           0
          </code>
          explicitly specifies the default unbounded behavior.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          MongoDB terminates operations that exceed their allotted time limit
using the same mechanism as
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.killOp/#mongodb-method-db.killOp">
           <code class="leafygreen-ui-1l06pbn">
            db.killOp()
           </code>
          </a>
          . MongoDB only
terminates an operation at one of its designated
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-interrupt-point">
           interrupt
points
           <!-- -->
           .
          </a>
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          findAndModify
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         string
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         The collection against which to run the command.
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          collation
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Specifies the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
           collation
          </a>
          to use for the operation.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
           Collation
          </a>
          allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          The collation option has the following syntax:
         </p>
         <script class="structured_data" type="application/ld+json">
          {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"collation: {\n   locale: &lt;string&gt;,\n   caseLevel: &lt;boolean&gt;,\n   caseFirst: &lt;string&gt;,\n   strength: &lt;int&gt;,\n   numericOrdering: &lt;boolean&gt;,\n   alternate: &lt;string&gt;,\n   maxVariable: &lt;string&gt;,\n   backwards: &lt;boolean&gt;\n}"}
         </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 none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">collation: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   locale: &lt;string&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   caseLevel: &lt;boolean&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   caseFirst: &lt;string&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   strength: &lt;int&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   numericOrdering: &lt;boolean&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   alternate: &lt;string&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   maxVariable: &lt;string&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   backwards: &lt;boolean&gt;</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">
          When specifying collation, the
          <code class="leafygreen-ui-1l06pbn">
           locale
          </code>
          field is mandatory; all
other collation fields are optional. For descriptions of the fields,
see
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation-document-fields">
           Collation Document
           <!-- -->
           .
          </a>
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If the collation is unspecified but the collection has a
default collation (see
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createCollection/#mongodb-method-db.createCollection">
           <code class="leafygreen-ui-1l06pbn">
            db.createCollection()
           </code>
          </a>
          ), the
operation uses the collation specified for the collection.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If no collation is specified for the collection or for the
operations, MongoDB uses the simple binary comparison used in prior
versions for string comparisons.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          You cannot specify multiple collations for an operation. For
example, you cannot specify different collations per field, or if
performing a find with a sort, you cannot use one collation for the
find and another for the sort.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          arrayFilters
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         array
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. An array of filter documents that determine which array elements to
modify for an update operation on an array field.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          In the update document, use the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/positional-filtered/#mongodb-update-up.---identifier--">
           <code class="leafygreen-ui-1l06pbn">
            $[&lt;identifier&gt;]
           </code>
          </a>
          filtered
positional operator to define an identifier, which you then reference
in the array filter documents. You cannot have an array filter
document for an identifier if the identifier is not included in the
update document.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          The
          <code class="leafygreen-ui-1l06pbn">
           &lt;identifier&gt;
          </code>
          must begin with a lowercase letter and
contain only alphanumeric characters.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          You can include the same identifier multiple times in the update
document; however, for each distinct identifier (
          <code class="leafygreen-ui-1l06pbn">
           $[identifier]
          </code>
          )
in the update document, you must specify
          <strong>
           exactly one
          </strong>
          corresponding array filter document. That is, you cannot specify
multiple array filter documents for the same identifier. For
example, if the update statement includes the identifier
          <code class="leafygreen-ui-1l06pbn">
           x
          </code>
          (possibly multiple times), you cannot specify the following for
          <code class="leafygreen-ui-1l06pbn">
           arrayFilters
          </code>
          that includes 2 separate filter documents for
          <code class="leafygreen-ui-1l06pbn">
           x
          </code>
          :
         </p>
         <script class="structured_data" type="application/ld+json">
          {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// INVALID\n\n[\n  { \"x.a\": { $gt: 85 } },\n  { \"x.b\": { $gt: 80 } }\n]","programmingLanguage":"JavaScript"}
         </script>
         <div class="css-1lju8ps">
          <div class="leafygreen-ui-196mwvg">
           <div class="leafygreen-ui-1jheeh3">
            <pre class="leafygreen-ui-1453az5" 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-comment">// INVALID</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"x.a"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"x.b"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">80</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
           </div>
          </div>
         </div>
         <p class="leafygreen-ui-1kp3ins">
          However, you can specify compound conditions on the same identifier
in a single filter document, such as in the following examples:
         </p>
         <script class="structured_data" type="application/ld+json">
          {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Example 1\n[\n  { $or: [{\"x.a\": {$gt: 85}}, {\"x.b\": {$gt: 80}}] }\n]\n// Example 2\n[\n  { $and: [{\"x.a\": {$gt: 85}}, {\"x.b\": {$gt: 80}}] }\n]\n// Example 3\n[\n  { \"x.a\": { $gt: 85 }, \"x.b\": { $gt: 80 } }\n]","programmingLanguage":"JavaScript"}
         </script>
         <div class="css-16lmvu9">
          <div class="leafygreen-ui-196mwvg">
           <div class="leafygreen-ui-1b1bvay">
            <pre class="leafygreen-ui-11itue3" 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-comment">// Example 1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">$or</span>: <!-- -->[<!-- -->{<span class="lg-highlight-string">"x.a"</span>: <!-- -->{<span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span>}}<!-- -->, <!-- -->{<span class="lg-highlight-string">"x.b"</span>: <!-- -->{<span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">80</span>}}] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Example 2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">$and</span>: <!-- -->[<!-- -->{<span class="lg-highlight-string">"x.a"</span>: <!-- -->{<span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span>}}<!-- -->, <!-- -->{<span class="lg-highlight-string">"x.b"</span>: <!-- -->{<span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">80</span>}}] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Example 3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-string">"x.a"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span> }<!-- -->, <span class="lg-highlight-string">"x.b"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">80</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">
          For examples, see
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-findAndModify-command-arrayFilters" target="_self">
           <span>
            Array Update Operations with
            <code class="leafygreen-ui-1l06pbn">
             arrayFilters
            </code>
            .
           </span>
          </a>
         </p>
         <p class="leafygreen-ui-1kp3ins">
          <code class="leafygreen-ui-1l06pbn">
           arrayFilters
          </code>
          is not available for updates that use an
aggregation pipeline.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          hint
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document or string
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. A document or string that specifies the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
           index
          </a>
          to use to support the
          <code class="leafygreen-ui-1l06pbn">
           query
          </code>
          .
         </p>
         <p class="leafygreen-ui-1kp3ins">
          The option can take an index specification document or the
index name string.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If you specify an index that does not exist, the operation
errors.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          For an example, see
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-ex-findAndModify-hint" target="_self">
           <span>
            Specify
            <code class="leafygreen-ui-1l06pbn">
             hint
            </code>
            for
            <code class="leafygreen-ui-1l06pbn">
             findAndModify
            </code>
            Operations
            <!-- -->
            .
           </span>
          </a>
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          comment
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         any
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Optional. A user-provided comment to attach to this command. Once set, this
comment appears alongside records of this command in the following
locations:
         </p>
         <ul class="leafygreen-ui-rioki0">
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
             mongod log messages
            </a>
            , in the
            <code class="leafygreen-ui-1l06pbn">
             attr.command.cursor.comment
            </code>
            field.
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
             Database profiler
            </a>
            output, in the
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.command">
             <code class="leafygreen-ui-1l06pbn">
              command.comment
             </code>
            </a>
            field.
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
             <code class="leafygreen-ui-1l06pbn">
              currentOp
             </code>
            </a>
            output, in the
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.command">
             <code class="leafygreen-ui-1l06pbn">
              command.comment
             </code>
            </a>
            field.
           </p>
          </li>
         </ul>
         <p class="leafygreen-ui-1kp3ins">
          A comment can be any valid
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
           BSON type
          </a>
          (string, integer, object, array, etc).
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-findAndModify-let-syntax" target="_self">
          <span>
           let
          </span>
         </a>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <span class="leafygreen-ui-1qnjd6d" id="std-label-findAndModify-let-syntax">
         </span>
         <p class="leafygreen-ui-1kp3ins">
          Optional.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Specifies a document with a list of variables. This allows you to
improve command readability by separating the variables from the query
text.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          The document syntax is:
         </p>
         <script class="structured_data" type="application/ld+json">
          {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  &lt;variable_name_1&gt;: &lt;expression_1&gt;,\n  ...,\n  &lt;variable_name_n&gt;: &lt;expression_n&gt;\n}","programmingLanguage":"JavaScript"}
         </script>
         <div class="css-1lju8ps">
          <div class="leafygreen-ui-196mwvg">
           <div class="leafygreen-ui-1jheeh3">
            <pre class="leafygreen-ui-1453az5" 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">  &lt;<!-- -->variable_name_1<!-- -->&gt;<!-- -->: &lt;<!-- -->expression_1<!-- -->&gt;<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->...<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  &lt;<!-- -->variable_name_n<!-- -->&gt;<!-- -->: &lt;<!-- -->expression_n<!-- -->&gt;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
           </div>
          </div>
         </div>
         <p class="leafygreen-ui-1kp3ins">
          The variable is set to the value returned by the expression, and cannot
be changed afterwards.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          To access the value of a variable in the command, use the double
dollar sign prefix (
          <code class="leafygreen-ui-1l06pbn">
           $$
          </code>
          ) together with your variable name in the form
          <code class="leafygreen-ui-1l06pbn">
           $$&lt;variable_name&gt;
          </code>
          . For example:
          <code class="leafygreen-ui-1l06pbn">
           $$targetTotal
          </code>
          .
         </p>
         <p class="leafygreen-ui-1kp3ins">
          To use a variable to filter results, you must access the variable
within the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
           <code class="leafygreen-ui-1l06pbn">
            $expr
           </code>
          </a>
          operator.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          For a complete example using
          <code class="leafygreen-ui-1l06pbn">
           let
          </code>
          and variables,
see
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-findAndModify-let-example" target="_self">
           <span>
            Use Variables in
            <code class="leafygreen-ui-1l06pbn">
             let
            </code>
            .
           </span>
          </a>
         </p>
         <div>
          <p>
           <em>
            New
            <!-- -->
            in version
            <!-- -->
            5.0
           </em>
           .
          </p>
         </div>
        </div>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Output
   <a class="headerlink leafygreen-ui-1kepx47" href="#output" 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="output">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      findAndModify
     </code>
    </span>
   </a>
   command returns a document with the
following fields:
  </p>
  <div class="leafygreen-ui-skkc39" tabindex="0">
   <table class="leafygreen-ui-hr8lsv">
    <colgroup>
     <col style="width:20%"/>
     <col style="width:20%"/>
     <col style="width:80%"/>
    </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>
         Field
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         Type
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         Description
        </div>
       </div>
      </th>
     </tr>
    </thead>
    <tbody>
     <tr class="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          value
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Contains the command's returned value. See
         <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-findandmodify-value-field" target="_self">
          <span>
           <code class="leafygreen-ui-1l06pbn">
            value
           </code>
          </span>
         </a>
         for details.
        </div>
       </div>
      </td>
     </tr>
     <tr class="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          lastErrorObject
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         document
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Contains information about updated documents. See
         <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-findandmodify-lastErrorObject" target="_self">
          <span>
           <code class="leafygreen-ui-1l06pbn">
            lastErrorObject
           </code>
          </span>
         </a>
         for details.
        </div>
       </div>
      </td>
     </tr>
     <tr class="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          ok
         </code>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         number
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Contains the command's execution status.
         <code class="leafygreen-ui-1l06pbn">
          1
         </code>
         on success, or
         <code class="leafygreen-ui-1l06pbn">
          0
         </code>
         if an
error occurred.
        </div>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-findandmodify-lastErrorObject">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    <code class="leafygreen-ui-1l06pbn">
     lastErrorObject
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#lasterrorobject" 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="lasterrorobject">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The
    <code class="leafygreen-ui-1l06pbn">
     lastErrorObject
    </code>
    embedded document contains the following fields:
   </p>
   <div class="leafygreen-ui-skkc39" tabindex="0">
    <table class="leafygreen-ui-hr8lsv">
     <colgroup>
      <col style="width:20%"/>
      <col style="width:20%"/>
      <col style="width:80%"/>
     </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>
          Field
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Type
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Description
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           n
          </code>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          integer
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          Contains the number of documents that matched the update
predicate or the number of documents that the command inserted or
deleted.
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           updatedExisting
          </code>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          boolean
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <p class="leafygreen-ui-1kp3ins">
           Contains
           <code class="leafygreen-ui-1l06pbn">
            true
           </code>
           if an
           <code class="leafygreen-ui-1l06pbn">
            update
           </code>
           operation:
          </p>
          <ul class="leafygreen-ui-rioki0">
           <li class="leafygreen-ui-d7p7b6">
            <p class="leafygreen-ui-1kp3ins">
             Updated an existing document.
            </p>
           </li>
           <li class="leafygreen-ui-d7p7b6">
            <p class="leafygreen-ui-1kp3ins">
             Found the document, but it was already in the
desired destination state so no update actually occurred.
            </p>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           upserted
          </code>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          document
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          Contains the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-objectid">
           ObjectId
          </a>
          of the inserted document if an
          <code class="leafygreen-ui-1l06pbn">
           update
          </code>
          operation with
          <code class="leafygreen-ui-1l06pbn">
           upsert: true
          </code>
          resulted in a new document.
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-findandmodify-value-field">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#value" 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="value">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    For
    <code class="leafygreen-ui-1l06pbn">
     remove
    </code>
    operations,
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    contains the removed document if
the query matches a document. If the query does not match a document to
remove,
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    contains
    <code class="leafygreen-ui-1l06pbn">
     null
    </code>
    .
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For
    <code class="leafygreen-ui-1l06pbn">
     update
    </code>
    operations, the
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    embedded document contains the
following:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If the
      <code class="leafygreen-ui-1l06pbn">
       new
      </code>
      parameter is not set or is
      <code class="leafygreen-ui-1l06pbn">
       false
      </code>
      :
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        the pre-modification document if the query matches a document;
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        otherwise,
        <code class="leafygreen-ui-1l06pbn">
         null
        </code>
        .
       </p>
      </li>
     </ul>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If
      <code class="leafygreen-ui-1l06pbn">
       new
      </code>
      is
      <code class="leafygreen-ui-1l06pbn">
       true
      </code>
      :
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        the updated document if the query returns a match;
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        the inserted document if
        <code class="leafygreen-ui-1l06pbn">
         upsert: true
        </code>
        and no document matches the query;
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        otherwise,
        <code class="leafygreen-ui-1l06pbn">
         null
        </code>
        .
       </p>
      </li>
     </ul>
    </li>
   </ul>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Behavior
   <a class="headerlink leafygreen-ui-1kepx47" href="#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="behavior">
    </div>
   </a>
  </h2>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-upsert-and-unique-index-dbcommand">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Upsert with Unique Index
    <a class="headerlink leafygreen-ui-1kepx47" href="#upsert-with-unique-index" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="upsert-with-unique-index">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Upserts can create duplicate documents, unless there is a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
     unique index
    </a>
    to prevent duplicates.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Consider an example where no document with the name
    <code class="leafygreen-ui-1l06pbn">
     Andy
    </code>
    exists
and multiple clients issue the following command at roughly the same
time:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"people\",\n     query: { name: \"Andy\" },\n     update: { $inc: { score: 1 } },\n     upsert: true\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"people"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Andy"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">upsert</span>: <span class="lg-highlight-literal">true</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 all
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    operations finish the query phase before any
client successfully inserts data,
    <strong>
     and
    </strong>
    there is no unique index on
the
    <code class="leafygreen-ui-1l06pbn">
     name
    </code>
    field, each
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    operation may result in an
insert, creating multiple documents with
    <code class="leafygreen-ui-1l06pbn">
     name: Andy
    </code>
    .
   </p>
   <p class="leafygreen-ui-1kp3ins">
    A unique index on the
    <code class="leafygreen-ui-1l06pbn">
     name
    </code>
    field ensures that only one document
is created. With a unique index in place, the multiple
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    operations now exhibit the following behavior:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Exactly one
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         findAndModify
        </code>
       </span>
      </a>
      operation will successfully insert a new
document.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Other
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         findAndModify
        </code>
       </span>
      </a>
      operations either update the newly-inserted
document or fail due to a unique key collision.
     </p>
     <p class="leafygreen-ui-1kp3ins">
      In order for other
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         findAndModify
        </code>
       </span>
      </a>
      operations to update the
newly-inserted document,
      <strong>
       all
      </strong>
      of the following conditions must
be met:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The target collection has a unique index that would cause a
duplicate key error.
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The update operation is not
        <code class="leafygreen-ui-1l06pbn">
         updateMany
        </code>
        or
        <code class="leafygreen-ui-1l06pbn">
         multi
        </code>
        is
        <code class="leafygreen-ui-1l06pbn">
         false
        </code>
        .
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The update match condition is either:
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          A single equality predicate. For example
          <code class="leafygreen-ui-1l06pbn">
           { "fieldA" : "valueA" }
          </code>
         </p>
        </li>
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          A logical AND of equality predicates. For example
          <code class="leafygreen-ui-1l06pbn">
           { "fieldA" :
"valueA", "fieldB" : "valueB" }
          </code>
         </p>
        </li>
       </ul>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The fields in the equality predicate match the fields in the
unique index key pattern.
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The update operation does not modify any fields in the
unique index key pattern.
       </p>
      </li>
     </ul>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The following table shows examples of
    <code class="leafygreen-ui-1l06pbn">
     upsert
    </code>
    operations that,
when a key collision occurs, either result in an update or fail.
   </p>
   <div class="leafygreen-ui-skkc39" tabindex="0">
    <table class="leafygreen-ui-hr8lsv">
     <colgroup>
      <col style="width:30%"/>
      <col style="width:40%"/>
      <col style="width:30%"/>
     </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>
          Unique Index Key Pattern
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Update Operation
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Result
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <script class="structured_data" type="application/ld+json">
           {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ name : 1 }","programmingLanguage":"JavaScript"}
          </script>
          <div class="css-1lju8ps">
           <div class="leafygreen-ui-196mwvg">
            <div class="leafygreen-ui-1jheeh3">
             <pre class="leafygreen-ui-1agdxuk" 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">{ <!-- -->name <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr></tbody></table></code></pre>
            </div>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <script class="structured_data" type="application/ld+json">
           {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.updateOne(\n   { name: \"Andy\" },\n   { $inc: { score: 1 } },\n   { upsert: true }\n)","programmingLanguage":"JavaScript"}
          </script>
          <div class="css-1lju8ps">
           <div class="leafygreen-ui-196mwvg">
            <div class="leafygreen-ui-tyg50d">
             <pre class="leafygreen-ui-1453az5" 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">people</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Andy"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">upsert</span>: <span class="lg-highlight-literal">true</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
            </div>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          The
          <code class="leafygreen-ui-1l06pbn">
           score
          </code>
          field of the matched document is incremented by
1.
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <script class="structured_data" type="application/ld+json">
           {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ name : 1 }","programmingLanguage":"JavaScript"}
          </script>
          <div class="css-1lju8ps">
           <div class="leafygreen-ui-196mwvg">
            <div class="leafygreen-ui-1jheeh3">
             <pre class="leafygreen-ui-1agdxuk" 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">{ <!-- -->name <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr></tbody></table></code></pre>
            </div>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <script class="structured_data" type="application/ld+json">
           {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.updateOne(\n   { name: { $ne: \"Joe\" } },\n   { $set: { name: \"Andy\" } },\n   { upsert: true }\n )","programmingLanguage":"JavaScript"}
          </script>
          <div class="css-1lju8ps">
           <div class="leafygreen-ui-196mwvg">
            <div class="leafygreen-ui-tyg50d">
             <pre class="leafygreen-ui-1453az5" 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">people</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">name</span>: <!-- -->{ <span class="lg-highlight-attr">$ne</span>: <span class="lg-highlight-string">"Joe"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Andy"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">upsert</span>: <span class="lg-highlight-literal">true</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr></tbody></table></code></pre>
            </div>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          The operation fails because it modifies the field in the
unique index key pattern (
          <code class="leafygreen-ui-1l06pbn">
           name
          </code>
          ).
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <script class="structured_data" type="application/ld+json">
           {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ name : 1 }","programmingLanguage":"JavaScript"}
          </script>
          <div class="css-1lju8ps">
           <div class="leafygreen-ui-196mwvg">
            <div class="leafygreen-ui-1jheeh3">
             <pre class="leafygreen-ui-1agdxuk" 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">{ <!-- -->name <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr></tbody></table></code></pre>
            </div>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <script class="structured_data" type="application/ld+json">
           {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.updateOne(\n  { name: \"Andy\", email: \"andy@xyz.com\" },\n  { $set: { active: false } },\n  { upsert: true }\n)","programmingLanguage":"JavaScript"}
          </script>
          <div class="css-1lju8ps">
           <div class="leafygreen-ui-196mwvg">
            <div class="leafygreen-ui-tyg50d">
             <pre class="leafygreen-ui-1453az5" 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">people</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Andy"</span>, <span class="lg-highlight-attr">email</span>: <span class="lg-highlight-string">"andy@xyz.com"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">active</span>: <span class="lg-highlight-literal">false</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">upsert</span>: <span class="lg-highlight-literal">true</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
            </div>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          The operation fails because the equality predicate fields
(
          <code class="leafygreen-ui-1l06pbn">
           name
          </code>
          ,
          <code class="leafygreen-ui-1l06pbn">
           email
          </code>
          ) do not match the index key field
(
          <code class="leafygreen-ui-1l06pbn">
           name
          </code>
          ).
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-cmd-findAndModify-sharded-collection">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Sharded Collections
    <a class="headerlink leafygreen-ui-1kepx47" href="#sharded-collections" 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="sharded-collections">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    To use
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    on a sharded collection:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If you only target one shard, you can use a partial shard key in the
      <code class="leafygreen-ui-1l06pbn">
       query
      </code>
      field or,
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      You can provide an equality condition on a full shard key in the
      <code class="leafygreen-ui-1l06pbn">
       query
      </code>
      field.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Starting in version 7.1, you do not need to provide the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
       shard key
      </a>
      or
      <code class="leafygreen-ui-1l06pbn">
       _id
      </code>
      field in the query specification.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Documents in a sharded collection can be
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-shard-key-missing">
     missing the shard key fields
    </a>
    . To target a
document that is missing the shard key, you can use the
    <code class="leafygreen-ui-1l06pbn">
     null
    </code>
    equality match
    <strong class="css-4dei7l">
     in conjunction with
    </strong>
    another filter condition
(such as on the
    <code class="leafygreen-ui-1l06pbn">
     _id
    </code>
    field). 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>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Shard Key Modification
     <a class="headerlink leafygreen-ui-1kepx47" href="#shard-key-modification" 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-modification">
      </div>
     </a>
    </h4>
    <p class="leafygreen-ui-1kp3ins">
     You can update a document's shard key value unless the shard key field is the
immutable
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     field.
    </p>
    <div class="leafygreen-ui-1sjx7ru" role="note">
     <h2 class="leafygreen-ui-29m78u">
      Warning
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       Documents in sharded collections can be missing the shard key fields.
Take precaution to avoid accidentally removing the shard key when changing
a document's shard key value.
      </p>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     To update the
     <strong>
      existing
     </strong>
     shard key value with
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        findAndModify
       </code>
       :
      </span>
     </a>
    </p>
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You
       <strong class="css-4dei7l">
        must
       </strong>
       run on a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
        <code class="leafygreen-ui-1l06pbn">
         mongos
        </code>
       </a>
       . Do
       <strong class="css-4dei7l">
        not
       </strong>
       issue the operation directly on the shard.
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You
       <strong class="css-4dei7l">
        must
       </strong>
       run either in a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
        transaction
       </a>
       or as a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/retryable-writes/">
        retryable write
        <!-- -->
        .
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You
       <strong class="css-4dei7l">
        must
       </strong>
       include an equality filter on the full shard key.
      </p>
     </li>
    </ul>
    <span class="leafygreen-ui-1qnjd6d" id="std-label-cmd-findAndModify-missing-shard-key">
    </span>
   </section>
   <section>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Missing Shard Key
     <a class="headerlink leafygreen-ui-1kepx47" href="#missing-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="missing-shard-key">
      </div>
     </a>
    </h4>
    <p class="leafygreen-ui-1kp3ins">
     Documents in a sharded collection can be
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-shard-key-missing">
      missing the shard key fields
     </a>
     . To use
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        findAndModify
       </code>
      </span>
     </a>
     to set the document's
     <strong>
      missing
     </strong>
     shard
key:
    </p>
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You
       <strong class="css-4dei7l">
        must
       </strong>
       run on a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
        <code class="leafygreen-ui-1l06pbn">
         mongos
        </code>
       </a>
       . Do
       <strong class="css-4dei7l">
        not
       </strong>
       issue the operation directly on the shard.
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You
       <strong class="css-4dei7l">
        must
       </strong>
       run either in a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
        transaction
       </a>
       or as a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/retryable-writes/">
        retryable write
       </a>
       if the new shard key value is not
       <code class="leafygreen-ui-1l06pbn">
        null
       </code>
       .
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You
       <strong class="css-4dei7l">
        must
       </strong>
       include an equality filter on the full shard key.
      </p>
     </li>
    </ul>
    <div class="leafygreen-ui-13vw3ie" role="note">
     <h2 class="leafygreen-ui-1fn4p2b">
      Tip
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       Since a missing key value is returned as part of a null equality
match, to avoid updating a null-valued key, include additional
query conditions (such as on the
       <code class="leafygreen-ui-1l06pbn">
        _id
       </code>
       field) as appropriate.
      </p>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     See also:
    </p>
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-shard-key-missing">
        Missing Shard Key Fields
       </a>
      </p>
     </li>
    </ul>
   </section>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Document Validation
    <a class="headerlink leafygreen-ui-1kepx47" href="#document-validation" 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="document-validation">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    command adds support for the
    <code class="leafygreen-ui-1l06pbn">
     bypassDocumentValidation
    </code>
    option, which lets you bypass
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/#std-label-schema-validation-overview">
     document validation
    </a>
    when
inserting or updating documents in a collection with validation
rules.
   </p>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-findAndModify-command-and-update">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Comparisons with the
    <code class="leafygreen-ui-1l06pbn">
     update
    </code>
    Method
    <a class="headerlink leafygreen-ui-1kepx47" href="#comparisons-with-the-update-method" 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="comparisons-with-the-update-method">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    When updating a document,
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    and the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
     <code class="leafygreen-ui-1l06pbn">
      updateOne()
     </code>
    </a>
    method operate differently:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If multiple documents match the update criteria, for
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         findAndModify
        </code>
       </span>
      </a>
      , you can specify a
      <code class="leafygreen-ui-1l06pbn">
       sort
      </code>
      to provide some
measure of control on which document to update.
     </p>
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
       <code class="leafygreen-ui-1l06pbn">
        updateOne()
       </code>
      </a>
      updates the first document that
matches.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      By default,
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         findAndModify
        </code>
       </span>
      </a>
      returns
      <!-- -->
      an object that contains the pre-modified version of the
document, as well as the status of the operation
      <!-- -->
      . To
obtain the updated document, use the
      <code class="leafygreen-ui-1l06pbn">
       new
      </code>
      option.
     </p>
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
       <code class="leafygreen-ui-1l06pbn">
        updateOne()
       </code>
      </a>
      method returns a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-method-WriteResult">
       <code class="leafygreen-ui-1l06pbn">
        WriteResult()
       </code>
      </a>
      object that contains the status of the operation.
     </p>
     <p class="leafygreen-ui-1kp3ins">
      To return the updated document, use the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
       <code class="leafygreen-ui-1l06pbn">
        find()
       </code>
      </a>
      method. However, other updates may have modified the document between
your update and the document retrieval. Also, if the update modified
only a single document but multiple documents matched, you will need to
use additional logic to identify the updated document.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    When modifying a
    <em>
     single
    </em>
    document, both
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    and the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
     <code class="leafygreen-ui-1l06pbn">
      updateOne()
     </code>
    </a>
    method
    <em>
     atomically
    </em>
    update the
document. See
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/write-operations-atomicity/">
     Atomicity and Transactions
    </a>
    for more
details about interactions and order of operations of these methods.
   </p>
   <div class="leafygreen-ui-13vw3ie" role="note">
    <h2 class="leafygreen-ui-1fn4p2b">
     Tip
    </h2>
    <h3 class="leafygreen-ui-144d5kf">
     <strong>
      See also:
     </strong>
    </h3>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/dot-dollar-considerations/#std-label-crud-concepts-dot-dollar-considerations">
       Considerations for field names
      </a>
     </p>
    </div>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Transactions
    <a class="headerlink leafygreen-ui-1kepx47" href="#transactions" 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="transactions">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    can be used inside
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
     distributed transactions
     <!-- -->
     .
    </a>
   </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">
      In most cases, a distributed transaction incurs a greater
performance cost over single document writes, and the
availability of distributed transactions should not be a
replacement for effective schema design. For many scenarios, the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/concepts/embedding-vs-references/#std-label-data-modeling-embedding">
       denormalized data model (embedded documents and arrays)
      </a>
      will continue to be optimal for your
data and use cases. That is, for many scenarios, modeling your data
appropriately will minimize the need for distributed
transactions.
     </p>
     <p class="leafygreen-ui-1kp3ins">
      For additional transactions usage considerations
(such as runtime limit and oplog size limit), see also
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions-production-consideration/#std-label-production-considerations">
       Production Considerations
       <!-- -->
       .
      </a>
     </p>
    </div>
   </div>
   <section>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Upsert within Transactions
     <a class="headerlink leafygreen-ui-1kepx47" href="#upsert-within-transactions" 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="upsert-within-transactions">
      </div>
     </a>
    </h4>
    <p class="leafygreen-ui-1kp3ins">
     You can create collections and indexes inside a
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-create-collections-indexes">
      distributed
transaction
     </a>
     if the
transaction is
     <strong class="css-4dei7l">
      not
     </strong>
     a cross-shard write transaction.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        findAndModify
       </code>
      </span>
     </a>
     with
     <code class="leafygreen-ui-1l06pbn">
      upsert: true
     </code>
     can be run on an existing
collection or a non-existing collection. If run on a non-existing
collection, the operation creates the collection.
    </p>
    <div class="leafygreen-ui-13vw3ie" role="note">
     <h2 class="leafygreen-ui-1fn4p2b">
      Tip
     </h2>
     <h3 class="leafygreen-ui-144d5kf">
      <strong>
       See also:
      </strong>
     </h3>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-create-collections-indexes">
        Create Collections and Indexes in a Transaction
       </a>
      </p>
     </div>
    </div>
   </section>
   <section>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Write Concerns and Transactions
     <a class="headerlink leafygreen-ui-1kepx47" href="#write-concerns-and-transactions" 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-concerns-and-transactions">
      </div>
     </a>
    </h4>
    <p class="leafygreen-ui-1kp3ins">
     Do not explicitly set the write concern for the operation if run in
a transaction. To use write concern with transactions, see
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-write-concern">
      Transactions and Write Concern
      <!-- -->
      .
     </a>
    </p>
   </section>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Examples
   <a class="headerlink leafygreen-ui-1kepx47" href="#examples" 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="examples">
    </div>
   </a>
  </h2>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Update and Return
    <a class="headerlink leafygreen-ui-1kepx47" href="#update-and-return" 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="update-and-return">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following command updates an existing document in the
    <code class="leafygreen-ui-1l06pbn">
     people
    </code>
    collection where the document matches the
    <code class="leafygreen-ui-1l06pbn">
     query
    </code>
    criteria:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"people\",\n     query: { name: \"Tom\", state: \"active\", rating: { $gt: 10 } },\n     sort: { rating: 1 },\n     update: { $inc: { score: 1 } }\n   }\n)","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"people"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Tom"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"active"</span>, <span class="lg-highlight-attr">rating</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">10</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</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">
    This command performs the following actions:
   </p>
   <ol class="leafygreen-ui-rioki0" type="1">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <code class="leafygreen-ui-1l06pbn">
       query
      </code>
      finds a document in the
      <code class="leafygreen-ui-1l06pbn">
       people
      </code>
      collection where the
      <code class="leafygreen-ui-1l06pbn">
       name
      </code>
      field has the value
      <code class="leafygreen-ui-1l06pbn">
       Tom
      </code>
      , the
      <code class="leafygreen-ui-1l06pbn">
       state
      </code>
      field has
the value
      <code class="leafygreen-ui-1l06pbn">
       active
      </code>
      and the
      <code class="leafygreen-ui-1l06pbn">
       rating
      </code>
      field has a value
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
       <code class="leafygreen-ui-1l06pbn">
        greater than
       </code>
      </a>
      10.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <code class="leafygreen-ui-1l06pbn">
       sort
      </code>
      orders the results of the query in ascending order.
If multiple documents meet the
      <code class="leafygreen-ui-1l06pbn">
       query
      </code>
      condition, the command will
select for modification the first document as ordered by this
      <code class="leafygreen-ui-1l06pbn">
       sort
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <code class="leafygreen-ui-1l06pbn">
       update
      </code>
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/inc/#mongodb-update-up.-inc">
       <code class="leafygreen-ui-1l06pbn">
        increments
       </code>
      </a>
      the value of the
      <code class="leafygreen-ui-1l06pbn">
       score
      </code>
      field by 1.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The command returns a document with the following fields:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The
        <code class="leafygreen-ui-1l06pbn">
         lastErrorObject
        </code>
        field that contains the details of the
command, including the field
        <code class="leafygreen-ui-1l06pbn">
         updatedExisting
        </code>
        which is
        <code class="leafygreen-ui-1l06pbn">
         true
        </code>
        , and
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        The
        <code class="leafygreen-ui-1l06pbn">
         value
        </code>
        field that contains the original (i.e.
pre-modification) document selected for this update:
       </p>
       <script class="structured_data" type="application/ld+json">
        {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"lastErrorObject\" : {\n     \"connectionId\" : 1,\n     \"updatedExisting\" : true,\n     \"n\" : 1,\n     \"syncMillis\" : 0,\n     \"writtenTo\" : null,\n     \"err\" : null,\n     \"ok\" : 1\n  },\n  value\" : {\n    \"_id\" : ObjectId(\"54f62d2885e4be1f982b9c9c\"),\n    \"name\" : \"Tom\",\n    \"state\" : \"active\",\n    \"rating\" : 100,\n    \"score\" : 5\n  },\n  \"ok\" : 1\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">"lastErrorObject"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"connectionId"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"updatedExisting"</span> <!-- -->: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"n"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"syncMillis"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"writtenTo"</span> <!-- -->: <span class="lg-highlight-literal">null</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"err"</span> <!-- -->: <span class="lg-highlight-literal">null</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->value<span class="lg-highlight-string">" : {</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">    "</span>_id<span class="lg-highlight-string">" : ObjectId("</span>54f62d2885e4be1f982b9c9c<span class="lg-highlight-string">"),</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">    "</span>name<span class="lg-highlight-string">" : "</span><span class="lg-highlight-title lg-highlight-class">Tom</span><span class="lg-highlight-string">",</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">    "</span>state<span class="lg-highlight-string">" : "</span>active<span class="lg-highlight-string">",</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">    "</span>rating<span class="lg-highlight-string">" : 100,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">    "</span>score<span class="lg-highlight-string">" : 5</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">  },</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">  "</span>ok<span class="lg-highlight-string">" : 1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">}</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>
      </li>
     </ul>
    </li>
   </ol>
   <p class="leafygreen-ui-1kp3ins">
    To return the updated document in the
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    field, add the
    <code class="leafygreen-ui-1l06pbn">
     new:true
    </code>
    option to the command.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    If no document match the
    <code class="leafygreen-ui-1l06pbn">
     query
    </code>
    condition, the command
returns a document that contains
    <code class="leafygreen-ui-1l06pbn">
     null
    </code>
    in the
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    field:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"value\" : null, \"ok\" : 1 }","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-string">"value"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</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">
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       mongosh
      </code>
     </span>
    </a>
    and many
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-driver">
     drivers
    </a>
    provide a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
     <code class="leafygreen-ui-1l06pbn">
      findAndModify()
     </code>
    </a>
    helper method.
Using the shell helper, this previous operation can take the
following form:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.findAndModify( {\n   query: { name: \"Tom\", state: \"active\", rating: { $gt: 10 } },\n   sort: { rating: 1 },\n   update: { $inc: { score: 1 } }\n} );","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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">people</span>.<span class="lg-highlight-title lg-highlight-function">findAndModify</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Tom"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"active"</span>, <span class="lg-highlight-attr">rating</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">10</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</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">
    However, the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
     <code class="leafygreen-ui-1l06pbn">
      findAndModify()
     </code>
    </a>
    shell helper
method returns only the unmodified document, or if
    <code class="leafygreen-ui-1l06pbn">
     new
    </code>
    is
    <code class="leafygreen-ui-1l06pbn">
     true
    </code>
    , the updated document.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"_id\" : ObjectId(\"54f62d2885e4be1f982b9c9c\"),\n   \"name\" : \"Tom\",\n   \"state\" : \"active\",\n   \"rating\" : 100,\n   \"score\" : 5\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">"54f62d2885e4be1f982b9c9c"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Tom"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"active"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"rating"</span> <!-- -->: <span class="lg-highlight-number">100</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"score"</span> <!-- -->: <span class="lg-highlight-number">5</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>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    <code class="leafygreen-ui-1l06pbn">
     upsert: true
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#upsert--true" 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="upsert--true">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    command includes the
    <code class="leafygreen-ui-1l06pbn">
     upsert:
true
    </code>
    option for the
    <code class="leafygreen-ui-1l06pbn">
     update
    </code>
    operation to either update a matching
document or, if no matching document exists, create a new document:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n               {\n                 findAndModify: \"people\",\n                 query: { name: \"Gus\", state: \"active\", rating: 100 },\n                 sort: { rating: 1 },\n                 update: { $inc: { score: 1 } },\n                 upsert: true\n               }\n             )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                 <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"people"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                 <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Gus"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"active"</span>, <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                 <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                 <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                 <span class="lg-highlight-attr">upsert</span>: <span class="lg-highlight-literal">true</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 command finds a matching document, the command performs an update.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    If the command does
    <strong>
     not
    </strong>
    find a matching document, the
    <code class="leafygreen-ui-1l06pbn">
     update
    </code>
    with
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-upsert">
     upsert: true
    </a>
    operation results in an insertion
and returns a document with the following fields:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <code class="leafygreen-ui-1l06pbn">
       lastErrorObject
      </code>
      field that contains the details of the
command, including the field
      <code class="leafygreen-ui-1l06pbn">
       upserted
      </code>
      that contains the
      <code class="leafygreen-ui-1l06pbn">
       _id
      </code>
      value of the newly inserted document, and
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <code class="leafygreen-ui-1l06pbn">
       value
      </code>
      field containing
      <code class="leafygreen-ui-1l06pbn">
       null
      </code>
      .
     </p>
    </li>
   </ul>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"value\" : null,\n  \"lastErrorObject\" : {\n     \"updatedExisting\" : false,\n     \"n\" : 1,\n     \"upserted\" : ObjectId(\"54f62c8bc85d4472eadea26f\")\n  },\n  \"ok\" : 1\n}","programmingLanguage":"Bash"}
   </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 bash 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">"value"</span> : null,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"lastErrorObject"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"updatedExisting"</span> : <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"n"</span> : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"upserted"</span> : ObjectId(<span class="lg-highlight-string">"54f62c8bc85d4472eadea26f"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"ok"</span> : 1</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>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Return New Document
    <a class="headerlink leafygreen-ui-1kepx47" href="#return-new-document" 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="return-new-document">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    command includes both
    <code class="leafygreen-ui-1l06pbn">
     upsert: true
    </code>
    option and the
    <code class="leafygreen-ui-1l06pbn">
     new:true
    </code>
    option. The command either
updates a matching document and returns the updated document or, if no
matching document exists, inserts a document and returns the newly
inserted document in the
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    field.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    In the following example, no document in the
    <code class="leafygreen-ui-1l06pbn">
     people
    </code>
    collection
matches the
    <code class="leafygreen-ui-1l06pbn">
     query
    </code>
    condition:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"people\",\n     query: { name: \"Pascal\", state: \"active\", rating: 25 },\n     sort: { rating: 1 },\n     update: { $inc: { score: 1 } },\n     upsert: true,\n     new: true\n   }\n)","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"people"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Pascal"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"active"</span>, <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">25</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">upsert</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">new</span>: <span class="lg-highlight-literal">true</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 command returns the newly inserted document in the
    <code class="leafygreen-ui-1l06pbn">
     value
    </code>
    field:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"lastErrorObject\" : {\n     \"connectionId\" : 1,\n     \"updatedExisting\" : false,\n     \"upserted\" : ObjectId(\"54f62bbfc85d4472eadea26d\"),\n     \"n\" : 1,\n     \"syncMillis\" : 0,\n     \"writtenTo\" : null,\n     \"err\" : null,\n     \"ok\" : 1\n  },\n  \"value\" : {\n     \"_id\" : ObjectId(\"54f62bbfc85d4472eadea26d\"),\n     \"name\" : \"Pascal\",\n     \"rating\" : 25,\n     \"state\" : \"active\",\n     \"score\" : 1\n  },\n  \"ok\" : 1\n}","programmingLanguage":"Bash"}
   </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 bash 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">"lastErrorObject"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"connectionId"</span> : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"updatedExisting"</span> : <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"upserted"</span> : ObjectId(<span class="lg-highlight-string">"54f62bbfc85d4472eadea26d"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"n"</span> : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"syncMillis"</span> : 0,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"writtenTo"</span> : null,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"err"</span> : null,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"ok"</span> : 1</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"value"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"_id"</span> : ObjectId(<span class="lg-highlight-string">"54f62bbfc85d4472eadea26d"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"name"</span> : <span class="lg-highlight-string">"Pascal"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"rating"</span> : 25,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"state"</span> : <span class="lg-highlight-string">"active"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"score"</span> : 1</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"ok"</span> : 1</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>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Sort and Remove
    <a class="headerlink leafygreen-ui-1kepx47" href="#sort-and-remove" 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="sort-and-remove">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    By including a
    <code class="leafygreen-ui-1l06pbn">
     sort
    </code>
    specification on the
    <code class="leafygreen-ui-1l06pbn">
     rating
    </code>
    field, the
following example removes from the
    <code class="leafygreen-ui-1l06pbn">
     people
    </code>
    collection a single
document with the
    <code class="leafygreen-ui-1l06pbn">
     state
    </code>
    value of
    <code class="leafygreen-ui-1l06pbn">
     active
    </code>
    and the lowest
    <code class="leafygreen-ui-1l06pbn">
     rating
    </code>
    among the matching documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"people\",\n     query: { state: \"active\" },\n     sort: { rating: 1 },\n     remove: true\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"people"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"active"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">remove</span>: <span class="lg-highlight-literal">true</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 command returns the deleted document:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"lastErrorObject\" : {\n     \"connectionId\" : 1,\n     \"n\" : 1,\n     \"syncMillis\" : 0,\n     \"writtenTo\" : null,\n     \"err\" : null,\n     \"ok\" : 1\n  },\n  \"value\" : {\n     \"_id\" : ObjectId(\"54f62a6785e4be1f982b9c9b\"),\n     \"name\" : \"XYZ123\",\n     \"score\" : 1,\n     \"state\" : \"active\",\n     \"rating\" : 3\n  },\n  \"ok\" : 1\n}","programmingLanguage":"Bash"}
   </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 bash 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">"lastErrorObject"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"connectionId"</span> : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"n"</span> : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"syncMillis"</span> : 0,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"writtenTo"</span> : null,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"err"</span> : null,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"ok"</span> : 1</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"value"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"_id"</span> : ObjectId(<span class="lg-highlight-string">"54f62a6785e4be1f982b9c9b"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"name"</span> : <span class="lg-highlight-string">"XYZ123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"score"</span> : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"state"</span> : <span class="lg-highlight-string">"active"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-string">"rating"</span> : 3</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"ok"</span> : 1</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>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Specify Collation
    <a class="headerlink leafygreen-ui-1kepx47" href="#specify-collation" 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="specify-collation">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
     Collation
    </a>
    allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    A collection
    <code class="leafygreen-ui-1l06pbn">
     myColl
    </code>
    has the following documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 1, category: \"café\", status: \"A\" }\n{ _id: 2, category: \"cafe\", status: \"a\" }\n{ _id: 3, category: \"cafE\", status: \"a\" }","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">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"café"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafe"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"a"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafE"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"a"</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 following operation includes the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
     collation
    </a>
    option:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"myColl\",\n     query: { category: \"cafe\", status: \"a\" },\n     sort: { category: 1 },\n     update: { $set: { status: \"Updated\" } },\n     collation: { locale: \"fr\", strength: 1 }\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"myColl"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafe"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"a"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"Updated"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">collation</span>: <!-- -->{ <span class="lg-highlight-attr">locale</span>: <span class="lg-highlight-string">"fr"</span>, <span class="lg-highlight-attr">strength</span>: <span class="lg-highlight-number">1</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 operation returns the following document:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"lastErrorObject\" : {\n      \"updatedExisting\" : true,\n      \"n\" : 1\n   },\n   \"value\" : {\n      \"_id\" : 1,\n      \"category\" : \"café\",\n      \"status\" : \"A\"\n   },\n   \"ok\" : 1\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">"lastErrorObject"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"updatedExisting"</span> <!-- -->: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"n"</span> <!-- -->: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"category"</span> <!-- -->: <span class="lg-highlight-string">"café"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</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>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-findAndModify-command-arrayFilters">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Array Update Operations with
    <code class="leafygreen-ui-1l06pbn">
     arrayFilters
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#array-update-operations-with-arrayfilters" 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="array-update-operations-with-arrayfilters">
     </div>
    </a>
   </h3>
   <div class="leafygreen-ui-15mdk4" role="note">
    <h2 class="leafygreen-ui-6n7b7d">
     Note
    </h2>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      <code class="leafygreen-ui-1l06pbn">
       arrayFilters
      </code>
      is not available for updates that use an
aggregation pipeline.
     </p>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    When updating an array field, you can specify
    <code class="leafygreen-ui-1l06pbn">
     arrayFilters
    </code>
    that
determine which array elements to update.
   </p>
   <section>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Update Elements Match
     <code class="leafygreen-ui-1l06pbn">
      arrayFilters
     </code>
     Criteria
     <a class="headerlink leafygreen-ui-1kepx47" href="#update-elements-match-arrayfilters-criteria" 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="update-elements-match-arrayfilters-criteria">
      </div>
     </a>
    </h4>
    <div class="leafygreen-ui-15mdk4" role="note">
     <h2 class="leafygreen-ui-6n7b7d">
      Note
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       <code class="leafygreen-ui-1l06pbn">
        arrayFilters
       </code>
       is not available for updates that use an
aggregation pipeline.
      </p>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     Create a collection
     <code class="leafygreen-ui-1l06pbn">
      students
     </code>
     with the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.insertMany( [\n   { \"_id\" : 1, \"grades\" : [ 95, 92, 90 ] },\n   { \"_id\" : 2, \"grades\" : [ 98, 100, 102 ] },\n   { \"_id\" : 3, \"grades\" : [ 95, 110, 100 ] }\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">db.<span class="lg-highlight-property">students</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">92</span>, <span class="lg-highlight-number">90</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">98</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">102</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">110</span>, <span class="lg-highlight-number">100</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">
     To update all elements that are greater than or equal to
     <code class="leafygreen-ui-1l06pbn">
      100
     </code>
     in the
     <code class="leafygreen-ui-1l06pbn">
      grades
     </code>
     array, use the positional
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/positional-filtered/#mongodb-update-up.---identifier--">
      <code class="leafygreen-ui-1l06pbn">
       $[&lt;identifier&gt;]
      </code>
     </a>
     operator with the
     <code class="leafygreen-ui-1l06pbn">
      arrayFilters
     </code>
     option:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"students\",\n     query: { grades: { $gte: 100 } },\n     update:  { $set: { \"grades.$[element]\" : 100 } },\n     arrayFilters: [ { \"element\": { $gte: 100 } } ]\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"students"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">grades</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">100</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>:  <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"grades.$[element]"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">arrayFilters</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"element"</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">100</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 operation updates the
     <code class="leafygreen-ui-1l06pbn">
      grades
     </code>
     field for a single document, and
after the operation, the collection has the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"grades\" : [ 95, 92, 90 ] }\n{ \"_id\" : 2, \"grades\" : [ 98, 100, 100 ] }\n{ \"_id\" : 3, \"grades\" : [ 95, 110, 100 ] }","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">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">92</span>, <span class="lg-highlight-number">90</span> ] }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">98</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">100</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">110</span>, <span class="lg-highlight-number">100</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>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Update Specific Elements of an Array of Documents
     <a class="headerlink leafygreen-ui-1kepx47" href="#update-specific-elements-of-an-array-of-documents" 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="update-specific-elements-of-an-array-of-documents">
      </div>
     </a>
    </h4>
    <div class="leafygreen-ui-15mdk4" role="note">
     <h2 class="leafygreen-ui-6n7b7d">
      Note
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       <code class="leafygreen-ui-1l06pbn">
        arrayFilters
       </code>
       is not available for updates that use an
aggregation pipeline.
      </p>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     Create a collection
     <code class="leafygreen-ui-1l06pbn">
      students2
     </code>
     with the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students2.insertMany( [\n   {\n      \"_id\" : 1,\n      \"grades\" : [\n         { \"grade\" : 80, \"mean\" : 75, \"std\" : 6 },\n         { \"grade\" : 85, \"mean\" : 90, \"std\" : 4 },\n         { \"grade\" : 85, \"mean\" : 85, \"std\" : 6 }\n      ]\n   },\n   {\n      \"_id\" : 2,\n      \"grades\" : [\n         { \"grade\" : 90, \"mean\" : 75, \"std\" : 6 },\n         { \"grade\" : 87, \"mean\" : 90, \"std\" : 3 },\n         { \"grade\" : 85, \"mean\" : 85, \"std\" : 4 }\n      ]\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">db.<span class="lg-highlight-property">students2</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</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-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</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-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">87</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">3</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</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></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 following operation finds a document where the
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     field equals
     <code class="leafygreen-ui-1l06pbn">
      1
     </code>
     and uses the filtered positional operator
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/positional-filtered/#mongodb-update-up.---identifier--">
      <code class="leafygreen-ui-1l06pbn">
       $[&lt;identifier&gt;]
      </code>
     </a>
     with
the
     <code class="leafygreen-ui-1l06pbn">
      arrayFilters
     </code>
     to update the
     <code class="leafygreen-ui-1l06pbn">
      mean
     </code>
     for all elements in the
     <code class="leafygreen-ui-1l06pbn">
      grades
     </code>
     array where the grade is greater than or equal to
     <code class="leafygreen-ui-1l06pbn">
      85
     </code>
     .
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"students2\",\n     query: { _id : 1 },\n     update: { $set: { \"grades.$[elem].mean\" : 100 } },\n     arrayFilters: [ { \"elem.grade\": { $gte: 85 } } ]\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"students2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"grades.$[elem].mean"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">arrayFilters</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"elem.grade"</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">85</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 operation updates the
     <code class="leafygreen-ui-1l06pbn">
      grades
     </code>
     field for a single document, and after the
operation, the collection has the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"_id\" : 1,\n   \"grades\" : [\n      { \"grade\" : 80, \"mean\" : 75, \"std\" : 6 },\n      { \"grade\" : 85, \"mean\" : 100, \"std\" : 4 },\n      { \"grade\" : 85, \"mean\" : 100, \"std\" : 6 }\n    ]\n}\n{\n   \"_id\" : 2,\n   \"grades\" : [\n      { \"grade\" : 90, \"mean\" : 75, \"std\" : 6 },\n      { \"grade\" : 87, \"mean\" : 90, \"std\" : 3 },\n      { \"grade\" : 85, \"mean\" : 85, \"std\" : 4 }\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-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</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-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">87</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">3</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</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>
    <span class="leafygreen-ui-1qnjd6d" id="std-label-cmdfindOneAndUpdate-agg-pipeline">
    </span>
   </section>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Use an Aggregation Pipeline for Updates
    <a class="headerlink leafygreen-ui-1kepx47" href="#use-an-aggregation-pipeline-for-updates" 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="use-an-aggregation-pipeline-for-updates">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.findAndModify" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       findAndModify
      </code>
     </span>
    </a>
    can accept an aggregation pipeline for the update.
The pipeline can consist of the following stages:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
       <code class="leafygreen-ui-1l06pbn">
        $addFields
       </code>
      </a>
      and its alias
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
       <code class="leafygreen-ui-1l06pbn">
        $set
       </code>
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
       <code class="leafygreen-ui-1l06pbn">
        $project
       </code>
      </a>
      and its alias
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
       <code class="leafygreen-ui-1l06pbn">
        $unset
       </code>
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
       <code class="leafygreen-ui-1l06pbn">
        $replaceRoot
       </code>
      </a>
      and its alias
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
       <code class="leafygreen-ui-1l06pbn">
        $replaceWith
       </code>
       .
      </a>
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Using the aggregation pipeline allows for a more expressive update
statement, such as expressing conditional updates based on current
field values or updating one field using the value of another field(s).
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For example, create a collection
    <code class="leafygreen-ui-1l06pbn">
     students2
    </code>
    with the following
documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students2.insertMany( [\n   {\n      \"_id\" : 1,\n      \"grades\" : [\n         { \"grade\" : 80, \"mean\" : 75, \"std\" : 6 },\n         { \"grade\" : 85, \"mean\" : 90, \"std\" : 4 },\n         { \"grade\" : 85, \"mean\" : 85, \"std\" : 6 }\n      ]\n   },\n   {\n      \"_id\" : 2,\n      \"grades\" : [\n         { \"grade\" : 90, \"mean\" : 75, \"std\" : 6 },\n         { \"grade\" : 87, \"mean\" : 90, \"std\" : 3 },\n         { \"grade\" : 85, \"mean\" : 85, \"std\" : 4 }\n      ]\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">db.<span class="lg-highlight-property">students2</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</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-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</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-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">87</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">3</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</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></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 following operation finds a document where the
    <code class="leafygreen-ui-1l06pbn">
     _id
    </code>
    field equals
    <code class="leafygreen-ui-1l06pbn">
     1
    </code>
    and uses an aggregation pipeline to calculate a new field
    <code class="leafygreen-ui-1l06pbn">
     total
    </code>
    from the
    <code class="leafygreen-ui-1l06pbn">
     grades
    </code>
    field:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     findAndModify: \"students2\",\n     query: {  \"_id\" : 1 },\n     update: [ { $set: { \"total\" : { $sum: \"$grades.grade\" } } } ],\n     new: true\n   }\n)","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"students2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{  <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">update</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"total"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$grades.grade"</span> } } } ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">new</span>: <span class="lg-highlight-literal">true</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>
   <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
      <code class="leafygreen-ui-1l06pbn">
       $set
      </code>
      used in the pipeline refers to the aggregation stage
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
       <code class="leafygreen-ui-1l06pbn">
        $set
       </code>
      </a>
      and not the update operator
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
       <code class="leafygreen-ui-1l06pbn">
        $set
       </code>
       .
      </a>
     </p>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    After the operation, the collection has the following documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : 1,\n  \"grades\" : [ { \"grade\" : 80, \"mean\" : 75, \"std\" : 6 }, { \"grade\" : 85, \"mean\" : 90, \"std\" : 4 }, { \"grade\" : 85, \"mean\" :85, \"std\" : 6 } ],\n  \"total\" : 250\n}\n{\n   \"_id\" : 2,\n   \"grades\" : [ { \"grade\" : 90, \"mean\" : 75, \"std\" : 6 }, { \"grade\" : 87, \"mean\" : 90, \"std\" : 3 }, { \"grade\" : 85, \"mean\" : 85,\"std\" : 4 } ]\n}","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->:<span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> } ]<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">250</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">   <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">87</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">3</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>,<span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</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>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-ex-findAndModify-hint">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Specify
    <code class="leafygreen-ui-1l06pbn">
     hint
    </code>
    for
    <code class="leafygreen-ui-1l06pbn">
     findAndModify
    </code>
    Operations
    <a class="headerlink leafygreen-ui-1kepx47" href="#specify-hint-for-findandmodify-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="specify-hint-for-findandmodify-operations">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    In
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       mongosh
      </code>
     </span>
    </a>
    , create a
    <code class="leafygreen-ui-1l06pbn">
     members
    </code>
    collection
with the following documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.members.insertMany( [\n   { \"_id\" : 1, \"member\" : \"abc123\", \"status\" : \"P\", \"points\" :  0,  \"misc1\" : null, \"misc2\" : null },\n   { \"_id\" : 2, \"member\" : \"xyz123\", \"status\" : \"A\", \"points\" : 60,  \"misc1\" : \"reminder: ping me at 100pts\", \"misc2\" : \"Some random comment\" },\n   { \"_id\" : 3, \"member\" : \"lmn123\", \"status\" : \"P\", \"points\" :  0,  \"misc1\" : null, \"misc2\" : null },\n   { \"_id\" : 4, \"member\" : \"pqr123\", \"status\" : \"D\", \"points\" : 20,  \"misc1\" : \"Deactivated\", \"misc2\" : null },\n   { \"_id\" : 5, \"member\" : \"ijk123\", \"status\" : \"P\", \"points\" :  0,  \"misc1\" : null, \"misc2\" : null },\n   { \"_id\" : 6, \"member\" : \"cde123\", \"status\" : \"A\", \"points\" : 86,  \"misc1\" : \"reminder: ping me at 100pts\", \"misc2\" : \"Some random comment\" }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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">members</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"member"</span> <!-- -->: <span class="lg-highlight-string">"abc123"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"P"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->:  <span class="lg-highlight-number">0</span>,  <span class="lg-highlight-string">"misc1"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"misc2"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"member"</span> <!-- -->: <span class="lg-highlight-string">"xyz123"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">60</span>,  <span class="lg-highlight-string">"misc1"</span> <!-- -->: <span class="lg-highlight-string">"reminder: ping me at 100pts"</span>, <span class="lg-highlight-string">"misc2"</span> <!-- -->: <span class="lg-highlight-string">"Some random comment"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"member"</span> <!-- -->: <span class="lg-highlight-string">"lmn123"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"P"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->:  <span class="lg-highlight-number">0</span>,  <span class="lg-highlight-string">"misc1"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"misc2"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"member"</span> <!-- -->: <span class="lg-highlight-string">"pqr123"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"D"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">20</span>,  <span class="lg-highlight-string">"misc1"</span> <!-- -->: <span class="lg-highlight-string">"Deactivated"</span>, <span class="lg-highlight-string">"misc2"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"member"</span> <!-- -->: <span class="lg-highlight-string">"ijk123"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"P"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->:  <span class="lg-highlight-number">0</span>,  <span class="lg-highlight-string">"misc1"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"misc2"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-string">"member"</span> <!-- -->: <span class="lg-highlight-string">"cde123"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">86</span>,  <span class="lg-highlight-string">"misc1"</span> <!-- -->: <span class="lg-highlight-string">"reminder: ping me at 100pts"</span>, <span class="lg-highlight-string">"misc2"</span> <!-- -->: <span class="lg-highlight-string">"Some random comment"</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">
    Create the following indexes on the collection:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.members.createIndex( { status: 1 } )\ndb.members.createIndex( { points: 1 } )","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">db.<span class="lg-highlight-property">members</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-number">1</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">members</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">1</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 following operation explicitly hints to use the index
    <code class="leafygreen-ui-1l06pbn">
     { status: 1 }
    </code>
    :
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand({\n   findAndModify: \"members\",\n   query: { \"points\": { $lte: 20 }, \"status\": \"P\" },\n   remove: true,\n   hint: { status: 1 }\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"members"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-string">"points"</span>: <!-- -->{ <span class="lg-highlight-attr">$lte</span>: <span class="lg-highlight-number">20</span> }<!-- -->, <span class="lg-highlight-string">"status"</span>: <span class="lg-highlight-string">"P"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">remove</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">hint</span>: <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">})</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <div class="leafygreen-ui-15mdk4" role="note">
    <h2 class="leafygreen-ui-6n7b7d">
     Note
    </h2>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      If you specify an index that does not exist, the operation errors.
     </p>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    To see the index used, run
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/explain/#mongodb-dbcommand-dbcmd.explain">
     <code class="leafygreen-ui-1l06pbn">
      explain
     </code>
    </a>
    on the operation:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n   {\n     explain: {\n       findAndModify: \"members\",\n       query: { \"points\": { $lte: 20 }, \"status\": \"P\" },\n       remove: true,\n       hint: { status: 1 }\n     },\n     verbosity: \"queryPlanner\"\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">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">explain</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">       <span class="lg-highlight-attr">findAndModify</span>: <span class="lg-highlight-string">"members"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">       <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-string">"points"</span>: <!-- -->{ <span class="lg-highlight-attr">$lte</span>: <span class="lg-highlight-number">20</span> }<!-- -->, <span class="lg-highlight-string">"status"</span>: <span class="lg-highlight-string">"P"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">       <span class="lg-highlight-attr">remove</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">       <span class="lg-highlight-attr">hint</span>: <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">verbosity</span>: <span class="lg-highlight-string">"queryPlanner"</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>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-findAndModify-let-example">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Use Variables in
    <code class="leafygreen-ui-1l06pbn">
     let
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-in-let" 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="use-variables-in-let">
     </div>
    </a>
   </h3>
   <div>
    <p>
     <em>
      New
      <!-- -->
      in version
      <!-- -->
      5.0
     </em>
     .
    </p>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    To define variables that you can access elsewhere in the command, use
the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-findAndModify-let-syntax" target="_self">
     <span>
      let
     </span>
    </a>
    option.
   </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">
      To filter results using a variable, you must access the variable
within the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
       <code class="leafygreen-ui-1l06pbn">
        $expr
       </code>
      </a>
      operator.
     </p>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    Create a collection
    <code class="leafygreen-ui-1l06pbn">
     cakeFlavors
    </code>
    :
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.insertMany( [\n   { _id: 1, flavor: \"chocolate\" },\n   { _id: 2, flavor: \"strawberry\" },\n   { _id: 3, flavor: \"cherry\" }\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">db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"chocolate"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"strawberry"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"cherry"</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 following example defines a
    <code class="leafygreen-ui-1l06pbn">
     targetFlavor
    </code>
    variable in
    <code class="leafygreen-ui-1l06pbn">
     let
    </code>
    and
uses the variable to change the cake flavor from cherry to orange:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.runCommand( {\n   findAndModify: db.cakeFlavors.getName(),\n   query: { $expr: { $eq: [ \"$flavor\", \"$$targetFlavor\" ] } },\n   update: { flavor: \"orange\" },\n   let: { targetFlavor: \"cherry\" }\n} )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" 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">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">findAndModify</span>: <!-- -->db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-string">"$flavor"</span>, <span class="lg-highlight-string">"$$targetFlavor"</span> ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"orange"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">let</span>: <!-- -->{ <span class="lg-highlight-attr">targetFlavor</span>: <span class="lg-highlight-string">"cherry"</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>
  </section>
 </section>
</section>
