<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="mysql-shell-tutorial-javascript-collections-add">
      </a>
      22.3.3.2 Working with Collections
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045127913088">
  </a>
  <p>
   To work with the collections in a schema, use the
   <code class="literal">
    db
   </code>
   global object to access the current
        schema. In this example we are using the
   <code class="literal">
    world_x
   </code>
   schema imported previously, and the
   <code class="literal">
    countryinfo
   </code>
   collection. Therefore, the format
        of the operations you issue is
   <code class="literal">
    db.
    <em class="replaceable">
     <code>
      collection_name
     </code>
    </em>
    .operation
   </code>
   ,
        where
   <em class="replaceable">
    <code>
     collection_name
    </code>
   </em>
   is the name of
        the collection which the operation is executed against. In the
        following examples, the operations are executed against the
   <code class="literal">
    countryinfo
   </code>
   collection.
  </p>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127906432">
       </a>
       Add a Document
      </h5>
     </div>
    </div>
   </div>
   <p>
    Use the
    <code class="literal">
     add()
    </code>
    method to insert one document
          or a list of documents into an existing collection. Insert the
          following document into the
    <code class="literal">
     countryinfo
    </code>
    collection. As this is multi-line content, press
    <span class="keycap">
     <strong>
      Enter
     </strong>
    </span>
    twice to insert the document.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa76666789"><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">add</span><span class="token punctuation">(</span>
 <span class="token punctuation">{</span>
    GNP<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token number">6</span><span class="token punctuation">,</span>
    IndepYear<span class="token punctuation">:</span> <span class="token number">1967</span><span class="token punctuation">,</span>
    Name<span class="token punctuation">:</span> <span class="token string">"Sealand"</span><span class="token punctuation">,</span>
    Code<span class="token punctuation">:</span> <span class="token string">"SEA"</span><span class="token punctuation">,</span>
    demographics<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        LifeExpectancy<span class="token punctuation">:</span> <span class="token number">79</span><span class="token punctuation">,</span>
        Population<span class="token punctuation">:</span> <span class="token number">27</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    geography<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        Continent<span class="token punctuation">:</span> <span class="token string">"Europe"</span><span class="token punctuation">,</span>
        Region<span class="token punctuation">:</span> <span class="token string">"British Islands"</span><span class="token punctuation">,</span>
        SurfaceArea<span class="token punctuation">:</span> <span class="token number">193</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    government<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        GovernmentForm<span class="token punctuation">:</span> <span class="token string">"Monarchy"</span><span class="token punctuation">,</span>
        HeadOfState<span class="token punctuation">:</span> <span class="token string">"Michael Bates"</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    The method returns the status of the operation. You can verify
          the operation by searching for the document. For example:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa4229650"><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">"GNP"</span><span class="token operator">:</span> <span class="token number">0.6</span><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>
   <p>
    Note that in addition to the fields specified when the
          document was added, there is one more field, the
    <code class="literal">
     _id
    </code>
    . Each document requires an identifier
          field called
    <code class="literal">
     _id
    </code>
    . The value of the
    <code class="literal">
     _id
    </code>
    field must be unique among all
          documents in the same collection. Document IDs are generated
          by the server, not the client, so MySQL Shell does not
          automatically set an
    <code class="literal">
     _id
    </code>
    value. MySQL sets
          an
    <code class="literal">
     _id
    </code>
    value if the document does not
          contain the
    <code class="literal">
     _id
    </code>
    field. For more
          information, see
    <a class="ulink" href="/doc/x-devapi-userguide/en/understanding-automatic-document-ids.html" target="_top">
     Understanding Document IDs
    </a>
    .
   </p>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127892352">
       </a>
       Related Information
      </h5>
     </div>
    </div>
   </div>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       See
       <a class="ulink" href="/doc/x-devapi-userguide/en/crud-ebnf-collection-crud-functions.html#crud-ebnf-collectionaddfunction" target="_top">
        CollectionAddFunction
       </a>
       for
              the full syntax definition.
      </p>
     </li>
     <li class="listitem">
      <p>
       See
       <a class="ulink" href="/doc/x-devapi-userguide/en/understanding-automatic-document-ids.html" target="_top">
        Understanding Document IDs
       </a>
       .
      </p>
     </li>
    </ul>
   </div>
  </div>
 </div>
 <br/>
</div>
