<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="mysql-shell-tutorial-javascript-documents-collections">
      </a>
      22.3.3 Documents and Collections
     </h3>
    </div>
   </div>
  </div>
  <div class="toc">
   <dl class="toc">
    <dt>
     <span class="section">
      <a href="mysql-shell-tutorial-javascript-collections-operations.html">
       22.3.3.1 Create, List, and Drop Collections
      </a>
     </span>
    </dt>
    <dt>
     <span class="section">
      <a href="mysql-shell-tutorial-javascript-collections-add.html">
       22.3.3.2 Working with Collections
      </a>
     </span>
    </dt>
    <dt>
     <span class="section">
      <a href="mysql-shell-tutorial-javascript-documents-find.html">
       22.3.3.3 Find Documents
      </a>
     </span>
    </dt>
    <dt>
     <span class="section">
      <a href="mysql-shell-tutorial-javascript-documents-modify.html">
       22.3.3.4 Modify Documents
      </a>
     </span>
    </dt>
    <dt>
     <span class="section">
      <a href="mysql-shell-tutorial-javascript-documents-remove.html">
       22.3.3.5 Remove Documents
      </a>
     </span>
    </dt>
    <dt>
     <span class="section">
      <a href="mysql-shell-tutorial-javascript-indexes-create.html">
       22.3.3.6 Create and Drop Indexes
      </a>
     </span>
    </dt>
   </dl>
  </div>
  <a class="indexterm" name="idm46045128015632">
  </a>
  <p>
   When you are using MySQL as a Document Store, collections are
      containers within a schema that you can create, list, and drop.
      Collections contain JSON documents that you can add, find, update,
      and remove.
  </p>
  <p>
   The examples in this section use the
   <code class="literal">
    countryinfo
   </code>
   collection in the
   <code class="literal">
    world_x
   </code>
   schema. For instructions on setting up
      the
   <code class="literal">
    world_x
   </code>
   schema, see
   <a class="xref" href="mysql-shell-tutorial-javascript-download.html" title="22.3.2 Download and Import world_x Database">
    Section 22.3.2, “Download and Import world_x Database”
   </a>
   .
  </p>
  <h4>
   <a name="mysql-shell-tutorial-javascript-documents">
   </a>
   Documents
  </h4>
  <p>
   In MySQL, documents are represented as JSON objects. Internally,
      they are stored in an efficient binary format that enables fast
      lookups and updates.
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Simple document format for JavaScript:
     </p>
     <pre class="screen">{field1: "value", field2 : 10, "field 3": null}
</pre>
    </li>
   </ul>
  </div>
  <p>
   An array of documents consists of a set of documents separated by
      commas and enclosed within
   <code class="literal">
    [
   </code>
   and
   <code class="literal">
    ]
   </code>
   characters.
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Simple array of documents for JavaScript:
     </p>
     <pre class="screen">[{"Name": "Aruba", "Code:": "ABW"}, {"Name": "Angola", "Code:": "AGO"}]
</pre>
    </li>
   </ul>
  </div>
  <p>
   MySQL supports the following JavaScript value types in JSON
      documents:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      numbers (integer and floating point)
     </p>
    </li>
    <li class="listitem">
     <p>
      strings
     </p>
    </li>
    <li class="listitem">
     <p>
      boolean (False and True)
     </p>
    </li>
    <li class="listitem">
     <p>
      null
     </p>
    </li>
    <li class="listitem">
     <p>
      arrays of more JSON values
     </p>
    </li>
    <li class="listitem">
     <p>
      nested (or embedded) objects of more JSON values
     </p>
    </li>
   </ul>
  </div>
  <h4>
   <a name="mysql-shell-tutorial-javascript-collections">
   </a>
   Collections
  </h4>
  <p>
   Collections are containers for documents that share a purpose and
      possibly share one or more indexes. Each collection has a unique
      name and exists within a single schema.
  </p>
  <p>
   The term schema is equivalent to a database, which means a group
      of database objects as opposed to a relational schema, used to
      enforce structure and constraints over data. A schema does not
      enforce conformity on the documents in a collection.
  </p>
  <p>
   In this quick-start guide:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Basic objects include:
     </p>
     <div class="informaltable">
      <table summary="Objects to use interactively in MySQL Shell">
       <colgroup>
        <col style="width: 40%"/>
        <col style="width: 60%"/>
       </colgroup>
       <thead>
        <tr>
         <th>
          Object form
         </th>
         <th>
          Description
         </th>
        </tr>
       </thead>
       <tbody>
        <tr>
         <td>
          <code class="literal">
           db
          </code>
         </td>
         <td>
          <code class="literal">
           db
          </code>
          is a global variable assigned to the current
                active schema. When you want to run operations against
                the schema, for example to retrieve a collection, you
                use methods available for the
          <code class="literal">
           db
          </code>
          variable.
         </td>
        </tr>
        <tr>
         <td>
          <code class="literal">
           db.getCollections()
          </code>
         </td>
         <td>
          <a class="link" href="mysql-shell-tutorial-javascript-collections-operations.html#mysql-shell-tutorial-javascript-collections-get" title="List Collections">
           db.getCollections()
          </a>
          returns a list of collections in the schema. Use the
                list to get references to collection objects, iterate
                over them, and so on.
         </td>
        </tr>
       </tbody>
      </table>
     </div>
    </li>
    <li class="listitem">
     <p>
      Basic operations scoped by collections include:
     </p>
     <div class="informaltable">
      <table summary="CRUD operations available in X DevAPI">
       <colgroup>
        <col style="width: 40%"/>
        <col style="width: 60%"/>
       </colgroup>
       <thead>
        <tr>
         <th>
          Operation form
         </th>
         <th>
          Description
         </th>
        </tr>
       </thead>
       <tbody>
        <tr>
         <td>
          <code class="literal">
           db.
           <em class="replaceable">
            <code>
             name
            </code>
           </em>
           .add()
          </code>
         </td>
         <td>
          The
          <a class="link" href="mysql-shell-tutorial-javascript-collections-add.html" title="22.3.3.2 Working with Collections">
           add()
          </a>
          method inserts one document or a list of documents into
                the named collection.
         </td>
        </tr>
        <tr>
         <td>
          <code class="literal">
           db.
           <em class="replaceable">
            <code>
             name
            </code>
           </em>
           .find()
          </code>
         </td>
         <td>
          The
          <a class="link" href="mysql-shell-tutorial-javascript-documents-find.html" title="22.3.3.3 Find Documents">
           find()
          </a>
          method returns some or all documents in the named
                collection.
         </td>
        </tr>
        <tr>
         <td>
          <code class="literal">
           db.
           <em class="replaceable">
            <code>
             name
            </code>
           </em>
           .modify()
          </code>
         </td>
         <td>
          The
          <a class="link" href="mysql-shell-tutorial-javascript-documents-modify.html" title="22.3.3.4 Modify Documents">
           modify()
          </a>
          method updates documents in the named collection.
         </td>
        </tr>
        <tr>
         <td>
          <code class="literal">
           db.
           <em class="replaceable">
            <code>
             name
            </code>
           </em>
           .remove()
          </code>
         </td>
         <td>
          The
          <a class="link" href="mysql-shell-tutorial-javascript-documents-remove.html" title="22.3.3.5 Remove Documents">
           remove()
          </a>
          method deletes one document or a list of documents from
                the named collection.
         </td>
        </tr>
       </tbody>
      </table>
     </div>
    </li>
   </ul>
  </div>
  <h4>
   <a name="idm46045127957440">
   </a>
   Related Information
  </h4>
  <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/devapi-users-working-with-collections.html" target="_top">
       Working with Collections
      </a>
      for a general overview.
     </p>
    </li>
    <li class="listitem">
     <p>
      <a class="ulink" href="/doc/x-devapi-userguide/en/mysql-x-crud-ebnf-definitions.html" target="_top">
       CRUD EBNF Definitions
      </a>
      provides a
          complete list of operations.
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
