<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="mysql-shell-tutorial-python-documents-find">
      </a>
      22.4.3.3 Find Documents
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045127179840">
  </a>
  <p>
   You can use the
   <code class="literal">
    find()
   </code>
   method to query for
        and return documents from a collection in a schema. MySQL Shell
        provides additional methods to use with the
   <code class="literal">
    find()
   </code>
   method to filter and sort the returned
        documents.
  </p>
  <p>
   MySQL provides the following operators to specify search
        conditions:
   <code class="literal">
    OR
   </code>
   (
   <code class="literal">
    ||
   </code>
   ),
   <code class="literal">
    AND
   </code>
   (
   <code class="literal">
    &amp;&amp;
   </code>
   ),
   <code class="literal">
    XOR
   </code>
   ,
   <code class="literal">
    IS
   </code>
   ,
   <code class="literal">
    NOT
   </code>
   ,
   <code class="literal">
    BETWEEN
   </code>
   ,
   <code class="literal">
    IN
   </code>
   ,
   <code class="literal">
    LIKE
   </code>
   ,
   <code class="literal">
    !=
   </code>
   ,
   <code class="literal">
    &lt;&gt;
   </code>
   ,
   <code class="literal">
    &gt;
   </code>
   ,
   <code class="literal">
    &gt;=
   </code>
   ,
   <code class="literal">
    &lt;
   </code>
   ,
   <code class="literal">
    &lt;=
   </code>
   ,
   <code class="literal">
    &amp;
   </code>
   ,
   <code class="literal">
    |
   </code>
   ,
   <code class="literal">
    &lt;&lt;
   </code>
   ,
   <code class="literal">
    &gt;&gt;
   </code>
   ,
   <code class="literal">
    +
   </code>
   ,
   <code class="literal">
    -
   </code>
   ,
   <code class="literal">
    *
   </code>
   ,
   <code class="literal">
    /
   </code>
   ,
   <code class="literal">
    ~
   </code>
   , and
   <code class="literal">
    %
   </code>
   .
  </p>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-python-documents-find-all">
       </a>
       Find All Documents in a Collection
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127156704">
   </a>
   <p>
    To return all documents in a collection, use the
    <code class="literal">
     find()
    </code>
    method without specifying search
          conditions. For example, the following operation returns all
          documents in the
    <code class="literal">
     countryinfo
    </code>
    collection.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa19345299"><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-py&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 punctuation">)</span>
<span class="token inlinejson"><span class="token punctuation">[</span>
     <span class="token punctuation">{</span>
          <span class="token property">"GNP"</span><span class="token operator">:</span> <span class="token number">828</span><span class="token punctuation">,</span>
          <span class="token property">"Code:"</span><span class="token operator">:</span> <span class="token string">"ABW"</span><span class="token punctuation">,</span>
          <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"Aruba"</span><span class="token punctuation">,</span>
          <span class="token property">"IndepYear"</span><span class="token operator">:</span> <span class="token null">null</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">"Continent"</span><span class="token operator">:</span> <span class="token string">"North America"</span><span class="token punctuation">,</span>
              <span class="token property">"Region"</span><span class="token operator">:</span> <span class="token string">"Caribbean"</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">"GovernmentForm"</span><span class="token operator">:</span> <span class="token string">"Nonmetropolitan Territory of The Netherlands"</span><span class="token punctuation">,</span>
              <span class="token property">"HeadOfState"</span><span class="token operator">:</span> <span class="token string">"Beatrix"</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">"LifeExpectancy"</span><span class="token operator">:</span> <span class="token number">78.4000015258789</span><span class="token punctuation">,</span>
              <span class="token property">"Population"</span><span class="token operator">:</span> <span class="token number">103000</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
          ...
      <span class="token punctuation">}</span>
 <span class="token punctuation">]</span></span>
<span class="token output">240 documents in set (0.00 sec)</span></code></pre>
   </div>
   <p>
    The method produces results that contain operational
          information in addition to all documents in the collection.
   </p>
   <p>
    An empty set (no matching documents) returns the following
          information:
   </p>
   <pre class="screen">Empty set (0.00 sec)
</pre>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-python-documents-find-filter-searches">
       </a>
       Filter Searches
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127147856">
   </a>
   <p>
    You can include search conditions with the
    <code class="literal">
     find()
    </code>
    method. The syntax for expressions
          that form a search condition is the same as that of
          traditional MySQL
    <a class="xref" href="functions.html" title="Chapter 14 Functions and Operators">
     Chapter 14,
     <i>
      Functions and Operators
     </i>
    </a>
    . You must
          enclose all expressions in quotes. For the sake of brevity,
          some of the examples do not display output.
   </p>
   <p>
    A simple search condition could consist of the
    <code class="literal">
     Name
    </code>
    field and a value we know is in a
          document. The following example returns a single document:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa78262362"><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-py&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 = 'Australia'"</span><span class="token punctuation">)</span>
<span class="token inlinejson"><span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token property">"GNP"</span><span class="token operator">:</span> <span class="token number">351182</span><span class="token punctuation">,</span>
        <span class="token property">"Code:"</span><span class="token operator">:</span> <span class="token string">"AUS"</span><span class="token punctuation">,</span>
        <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"Australia"</span><span class="token punctuation">,</span>
        <span class="token property">"IndepYear"</span><span class="token operator">:</span> <span class="token number">1901</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">"Continent"</span><span class="token operator">:</span> <span class="token string">"Oceania"</span><span class="token punctuation">,</span>
            <span class="token property">"Region"</span><span class="token operator">:</span> <span class="token string">"Australia and New Zealand"</span><span class="token punctuation">,</span>
            <span class="token property">"SurfaceArea"</span><span class="token operator">:</span> <span class="token number">7741220</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">"GovernmentForm"</span><span class="token operator">:</span> <span class="token string">"Constitutional Monarchy, Federation"</span><span class="token punctuation">,</span>
            <span class="token property">"HeadOfState"</span><span class="token operator">:</span> <span class="token string">"Elisabeth II"</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">"LifeExpectancy"</span><span class="token operator">:</span> <span class="token number">79.80000305175781</span><span class="token punctuation">,</span>
            <span class="token property">"Population"</span><span class="token operator">:</span> <span class="token number">18886000</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span></span></code></pre>
   </div>
   <p>
    The following example searches for all countries that have a
          GNP higher than $500 billion. The
    <code class="literal">
     countryinfo
    </code>
    collection measures GNP in
          units of million.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa161517"><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-py&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">"GNP &gt; 500000"</span><span class="token punctuation">)</span>
<span class="token operator">...</span><span class="token punctuation">[</span><em>output removed</em><span class="token punctuation">]</span>
<span class="token output">10 documents in set (0.00 sec)</span></code></pre>
   </div>
   <p>
    The Population field in the following query is embedded within
          the demographics object. To access the embedded field, use a
          period between demographics and Population to identify the
          relationship. Document and field names are case-sensitive.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa66592794"><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-py&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">"GNP &gt; 500000 and demographics.Population &lt; 100000000"</span><span class="token punctuation">)</span>
<span class="token operator">...</span><span class="token punctuation">[</span><em>output removed</em><span class="token punctuation">]</span>
<span class="token output">6 documents in set (0.00 sec)</span></code></pre>
   </div>
   <p>
    Arithmetic operators in the following expression are used to
          query for countries with a GNP per capita higher than $30000.
          Search conditions can include arithmetic operators and most
          MySQL functions.
   </p>
   <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Note
    </div>
    <p>
     Seven documents in the
     <code class="literal">
      countryinfo
     </code>
     collection have a population value of zero. Therefore
            warning messages appear at the end of the output.
    </p>
   </div>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa519534"><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-py&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">"GNP*1000000/demographics.Population &gt; 30000"</span><span class="token punctuation">)</span>
<span class="token operator">...</span><span class="token punctuation">[</span><em>output removed</em><span class="token punctuation">]</span>
<span class="token output">9 documents in set, 7 warnings (0.00 sec)</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span>
Warning <span class="token punctuation">(</span>Code <span class="token number">1365</span><span class="token punctuation">)</span><span class="token punctuation">:</span> Division by <span class="token number">0</span></code></pre>
   </div>
   <p>
    You can separate a value from the search condition by using
          the
    <code class="literal">
     bind()
    </code>
    method. For example, instead of
          specifying a hard-coded country name as the condition,
          substitute a named placeholder consisting of a colon followed
          by a name that begins with a letter, such as
    <span class="emphasis">
     <em>
      country
     </em>
    </span>
    . Then use the
    <code class="literal">
     bind(
     <em class="replaceable">
      <code>
       placeholder
      </code>
     </em>
     ,
     <em class="replaceable">
      <code>
       value
      </code>
     </em>
     )
    </code>
    method as follows:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa20709129"><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-py&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 = :country"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token string">"country"</span><span class="token punctuation">,</span> <span class="token string">"Italy"</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">1161755</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"00005de917d8000000000000006a"</span><span class="token punctuation">,</span>
    <span class="token property">"Code"</span><span class="token operator">:</span> <span class="token string">"ITA"</span><span class="token punctuation">,</span>
    <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"Italy"</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 punctuation">]</span><span class="token punctuation">,</span>
    <span class="token property">"IndepYear"</span><span class="token operator">:</span> <span class="token number">1861</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">"Southern 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">301316</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">"Carlo Azeglio Ciampi"</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">57680000</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>
<span class="token output">1 document in set (0.01 sec)</span></code></pre>
   </div>
   <div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Tip
    </div>
    <p>
     Within a program, binding enables you to specify
            placeholders in your expressions, which are filled in with
            values before execution and can benefit from automatic
            escaping, as appropriate.
    </p>
    <p>
     Always use binding to sanitize input. Avoid introducing
            values in queries using string concatenation, which can
            produce invalid input and, in some cases, can cause security
            issues.
    </p>
   </div>
   <p>
    You can use placeholders and the
    <code class="literal">
     bind()
    </code>
    method to create saved searches which you can then call with
          different values. For example to create a saved search for a
          country:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa33728926"><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-py&gt;</span> myFind <span class="token operator">=</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 = :country"</span><span class="token punctuation">)</span>
<span class="token prompt">mysql-py&gt;</span> myFind<span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token string">'country'</span><span class="token punctuation">,</span> <span class="token string">'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">"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>
<span class="token output">1 document in set (0.0028 sec)</span>

<span class="token prompt">mysql-py&gt;</span> myFind<span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token string">'country'</span><span class="token punctuation">,</span> <span class="token string">'Germany'</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">2133367</span><span class="token punctuation">,</span>
    <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"00005de917d80000000000000038"</span><span class="token punctuation">,</span>
    <span class="token property">"Code"</span><span class="token operator">:</span> <span class="token string">"DEU"</span><span class="token punctuation">,</span>
    <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"Germany"</span><span class="token punctuation">,</span>
    <span class="token property">"IndepYear"</span><span class="token operator">:</span> <span class="token number">1955</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">357022</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">"Johannes Rau"</span><span class="token punctuation">,</span>
        <span class="token property">"GovernmentForm"</span><span class="token operator">:</span> <span class="token string">"Federal 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">82164700</span><span class="token punctuation">,</span>
        <span class="token property">"LifeExpectancy"</span><span class="token operator">:</span> <span class="token number">77.4000015258789</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></span>

<span class="token output">1 document in set (0.0026 sec)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127116016">
       </a>
       Project Results
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127115344">
   </a>
   <p>
    You can return specific fields of a document, instead of
          returning all the fields. The following example returns the
          GNP and Name fields of all documents in the
    <code class="literal">
     countryinfo
    </code>
    collection matching the search
          conditions.
   </p>
   <p>
    Use the
    <code class="literal">
     fields()
    </code>
    method to pass the list of
          fields to return.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa97442360"><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-py&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">"GNP &gt; 5000000"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fields</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"GNP"</span><span class="token punctuation">,</span> <span class="token string">"Name"</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token inlinejson"><span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token property">"GNP"</span><span class="token operator">:</span> <span class="token number">8510700</span><span class="token punctuation">,</span>
        <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"United States"</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span></span>
<span class="token output">1 document in set (0.00 sec)</span></code></pre>
   </div>
   <p>
    In addition, you can alter the returned
          documents—adding, renaming, nesting and even computing
          new field values—with an expression that describes the
          document to return. For example, alter the names of the fields
          with the following expression to return only two documents.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa23751052"><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-py&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 punctuation">)</span><span class="token punctuation">.</span><span class="token function">fields</span><span class="token punctuation">(</span>
mysqlx<span class="token punctuation">.</span><span class="token function">expr</span><span class="token punctuation">(</span><span class="token string">'{"Name": upper(Name), "GNPPerCapita": GNP*1000000/demographics.Population}'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token inlinejson"><span class="token punctuation">{</span>
    <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"ARUBA"</span><span class="token punctuation">,</span>
    <span class="token property">"GNPPerCapita"</span><span class="token operator">:</span> <span class="token number">8038.834951456311</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
    <span class="token property">"Name"</span><span class="token operator">:</span> <span class="token string">"AFGHANISTAN"</span><span class="token punctuation">,</span>
    <span class="token property">"GNPPerCapita"</span><span class="token operator">:</span> <span class="token number">263.0281690140845</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="idm46045127106256">
       </a>
       Limit, Sort, and Skip Results
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127105568">
   </a>
   <p>
    You can apply the
    <code class="literal">
     limit()
    </code>
    ,
    <code class="literal">
     sort()
    </code>
    , and
    <code class="literal">
     skip()
    </code>
    methods to manage the number and order of documents returned
          by the
    <code class="literal">
     find()
    </code>
    method.
   </p>
   <p>
    To specify the number of documents included in a result set,
          append the
    <code class="literal">
     limit()
    </code>
    method with a value to
          the
    <code class="literal">
     find()
    </code>
    method. The following query
          returns the first five documents in the
    <code class="literal">
     countryinfo
    </code>
    collection.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa4303978"><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-py&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 punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token operator">...</span> <span class="token punctuation">[</span><em>output removed</em><span class="token punctuation">]</span>
<span class="token output">5 documents in set (0.00 sec)</span></code></pre>
   </div>
   <p>
    To specify an order for the results, append the
    <code class="literal">
     sort()
    </code>
    method to the
    <code class="literal">
     find()
    </code>
    method. Pass to the
    <code class="literal">
     sort()
    </code>
    method a list of one or more fields
          to sort by and, optionally, the descending
          (
    <code class="literal">
     desc
    </code>
    ) or ascending
          (
    <code class="literal">
     asc
    </code>
    ) attribute as appropriate. Ascending
          order is the default order type.
   </p>
   <p>
    For example, the following query sorts all documents by the
          IndepYear field and then returns the first eight documents in
          descending order.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa38315623"><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-py&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 punctuation">)</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"IndepYear desc"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span>
<span class="token operator">...</span> <span class="token punctuation">[</span><em>output removed</em><span class="token punctuation">]</span>
<span class="token output">8 documents in set (0.00 sec)</span></code></pre>
   </div>
   <p>
    By default, the
    <code class="literal">
     limit()
    </code>
    method starts from
          the first document in the collection. You can use the
    <code class="literal">
     skip()
    </code>
    method to change the starting
          document. For example, to ignore the first document and return
          the next eight documents matching the condition, pass to the
    <code class="literal">
     skip()
    </code>
    method a value of 1.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa42339326"><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-py&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 punctuation">)</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"IndepYear desc"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token operator">...</span> <span class="token punctuation">[</span><em>output removed</em><span class="token punctuation">]</span>
<span class="token output">8 documents in set (0.00 sec)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127083568">
       </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="functions.html" title="Chapter 14 Functions and Operators">
        MySQL Reference
              Manual
       </a>
       provides detailed documentation on functions
              and operators.
      </p>
     </li>
     <li class="listitem">
      <p>
       See
       <a class="ulink" href="/doc/x-devapi-userguide/en/crud-ebnf-collection-crud-functions.html#crud-ebnf-collectionfindfunction" target="_top">
        CollectionFindFunction
       </a>
       for
              the full syntax definition.
      </p>
     </li>
    </ul>
   </div>
  </div>
 </div>
 <br/>
</div>
