<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="mysql-shell-tutorial-javascript-documents-modify">
      </a>
      22.3.3.4 Modify Documents
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045127784432">
  </a>
  <p>
   You can use the
   <code class="literal">
    modify()
   </code>
   method to update one
        or more documents in a collection. The X DevAPI provides
        additional methods for use with the
   <code class="literal">
    modify()
   </code>
   method to:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Set and unset fields within documents.
     </p>
    </li>
    <li class="listitem">
     <p>
      Append, insert, and delete arrays.
     </p>
    </li>
    <li class="listitem">
     <p>
      Bind, limit, and sort the documents to be modified.
     </p>
    </li>
   </ul>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127777984">
       </a>
       Set and Unset Document Fields
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127777296">
   </a>
   <p>
    The
    <code class="literal">
     modify()
    </code>
    method works by filtering a
          collection to include only the documents to be modified and
          then applying the operations that you specify to those
          documents.
   </p>
   <p>
    In the following example, the
    <code class="literal">
     modify()
    </code>
    method uses the search condition to identify the document to
          change and then the
    <code class="literal">
     set()
    </code>
    method replaces
          two values within the nested demographics object.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa99452794"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">modify</span><span class="token punctuation">(</span><span class="token string">"Code = 'SEA'"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span>
<span class="token string">"demographics"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">"LifeExpectancy"</span><span class="token punctuation">:</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token string">"Population"</span><span class="token punctuation">:</span> <span class="token number">28</span><span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    After you modify a document, use the
    <code class="literal">
     find()
    </code>
    method to verify the change.
   </p>
   <p>
    To remove content from a document, use the
    <code class="literal">
     modify()
    </code>
    and
    <code class="literal">
     unset()
    </code>
    methods. For example, the following query removes the GNP from
          a document that matches the search condition.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa88853952"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">modify</span><span class="token punctuation">(</span><span class="token string">"Name = 'Sealand'"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unset</span><span class="token punctuation">(</span><span class="token string">"GNP"</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    Use the
    <code class="literal">
     find()
    </code>
    method to verify the change.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa44365245"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">"Name = 'Sealand'"</span><span class="token punctuation">)</span>
<span class="token inlinejson"><span class="token punctuation">{</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"00005e2ff4af00000000000000f4"</span><span class="token punctuation">,</span>
    <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"Sealand"</span><span class="token punctuation">,</span>
    <span class="token property">"Code:"</span><span class="token operator">:</span> <span class="token string">"SEA"</span><span class="token punctuation">,</span>
    <span class="token property">"IndepYear"</span><span class="token operator">:</span> <span class="token number">1967</span><span class="token punctuation">,</span>
    <span class="token property">"geography"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"Region"</span><span class="token operator">:</span> <span class="token string">"British Islands"</span><span class="token punctuation">,</span>
        <span class="token property">"Continent"</span><span class="token operator">:</span> <span class="token string">"Europe"</span><span class="token punctuation">,</span>
        <span class="token property">"SurfaceArea"</span><span class="token operator">:</span> <span class="token number">193</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"government"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"HeadOfState"</span><span class="token operator">:</span> <span class="token string">"Michael Bates"</span><span class="token punctuation">,</span>
        <span class="token property">"GovernmentForm"</span><span class="token operator">:</span> <span class="token string">"Monarchy"</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"demographics"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"Population"</span><span class="token operator">:</span> <span class="token number">27</span><span class="token punctuation">,</span>
        <span class="token property">"LifeExpectancy"</span><span class="token operator">:</span> <span class="token number">79</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-javascript-append-insert-delete">
       </a>
       Append, Insert, and Delete Arrays
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127761264">
   </a>
   <p>
    To append an element to an array field, or insert, or delete
          elements in an array, use the
    <code class="literal">
     arrayAppend()
    </code>
    ,
    <code class="literal">
     arrayInsert()
    </code>
    , or
    <code class="literal">
     arrayDelete()
    </code>
    methods. The following
          examples modify the
    <code class="literal">
     countryinfo
    </code>
    collection
          to enable tracking of international airports.
   </p>
   <p>
    The first example uses the
    <code class="literal">
     modify()
    </code>
    and
    <code class="literal">
     set()
    </code>
    methods to create a new Airports
          field in all documents.
   </p>
   <div class="caution" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Caution
    </div>
    <p>
     Use care when you modify documents without specifying a
            search condition; doing so modifies all documents in the
            collection.
    </p>
   </div>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa78636580"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">modify</span><span class="token punctuation">(</span><span class="token string">"true"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">"Airports"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    With the Airports field added, the next example uses the
    <code class="literal">
     arrayAppend()
    </code>
    method to add a new airport
          to one of the documents.
    <span class="emphasis">
     <em>
      $.Airports
     </em>
    </span>
    in
          the following example represents the Airports field of the
          current document.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa88978832"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">modify</span><span class="token punctuation">(</span><span class="token string">"Name = 'France'"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">arrayAppend</span><span class="token punctuation">(</span><span class="token string">"$.Airports"</span><span class="token punctuation">,</span> <span class="token string">"ORY"</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    Use
    <code class="literal">
     find()
    </code>
    to see the change.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa2315023"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">"Name = 'France'"</span><span class="token punctuation">)</span>
<span class="token inlinejson"><span class="token punctuation">{</span>
    <span class="token property">"GNP"</span><span class="token operator">:</span> <span class="token number">1424285</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"00005de917d80000000000000048"</span><span class="token punctuation">,</span>
    <span class="token property">"Code"</span><span class="token operator">:</span> <span class="token string">"FRA"</span><span class="token punctuation">,</span>
    <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"France"</span><span class="token punctuation">,</span>
    <span class="token property">"Airports"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token string">"ORY"</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token property">"IndepYear"</span><span class="token operator">:</span> <span class="token number">843</span><span class="token punctuation">,</span>
    <span class="token property">"geography"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"Region"</span><span class="token operator">:</span> <span class="token string">"Western Europe"</span><span class="token punctuation">,</span>
        <span class="token property">"Continent"</span><span class="token operator">:</span> <span class="token string">"Europe"</span><span class="token punctuation">,</span>
        <span class="token property">"SurfaceArea"</span><span class="token operator">:</span> <span class="token number">551500</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"government"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"HeadOfState"</span><span class="token operator">:</span> <span class="token string">"Jacques Chirac"</span><span class="token punctuation">,</span>
        <span class="token property">"GovernmentForm"</span><span class="token operator">:</span> <span class="token string">"Republic"</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"demographics"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"Population"</span><span class="token operator">:</span> <span class="token number">59225700</span><span class="token punctuation">,</span>
        <span class="token property">"LifeExpectancy"</span><span class="token operator">:</span> <span class="token number">78.80000305175781</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></span></code></pre>
   </div>
   <p>
    To insert an element at a different position in the array, use
          the
    <code class="literal">
     arrayInsert()
    </code>
    method to specify which
          index to insert in the path expression. In this case, the
          index is 0, or the first element in the array.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa47422024"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">modify</span><span class="token punctuation">(</span><span class="token string">"Name = 'France'"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">arrayInsert</span><span class="token punctuation">(</span><span class="token string">"$.Airports[0]"</span><span class="token punctuation">,</span> <span class="token string">"CDG"</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    To delete an element from the array, you must pass to the
    <code class="literal">
     arrayDelete()
    </code>
    method the index of the
          element to be deleted.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa58454430"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-js&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">modify</span><span class="token punctuation">(</span><span class="token string">"Name = 'France'"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">arrayDelete</span><span class="token punctuation">(</span><span class="token string">"$.Airports[1]"</span><span class="token punctuation">)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127738768">
       </a>
       Related Information
      </h5>
     </div>
    </div>
   </div>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       The
       <a class="link" href="json.html#json-paths" title="Searching and Modifying JSON Values">
        MySQL Reference
              Manual
       </a>
       provides instructions to help you search for
              and modify JSON values.
      </p>
     </li>
     <li class="listitem">
      <p>
       See
       <a class="ulink" href="/doc/x-devapi-userguide/en/crud-ebnf-collection-crud-functions.html#crud-ebnf-collectionmodifyfunction" target="_top">
        CollectionModifyFunction
       </a>
       for the full syntax definition.
      </p>
     </li>
    </ul>
   </div>
  </div>
 </div>
 <br/>
</div>
