<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.mongodb-driver-query.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'mongodb-driver-query.construct.php',
    1 => 'MongoDB\\Driver\\Query::__construct',
    2 => 'Create a new Query',
  ),
  'up' => 
  array (
    0 => 'class.mongodb-driver-query.php',
    1 => 'MongoDB\\Driver\\Query',
  ),
  'prev' => 
  array (
    0 => 'class.mongodb-driver-query.php',
    1 => 'MongoDB\\Driver\\Query',
  ),
  'next' => 
  array (
    0 => 'class.mongodb-driver-bulkwrite.php',
    1 => 'MongoDB\\Driver\\BulkWrite',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mongodb/mongodb/driver/query/construct.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mongodb-driver-query.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\Driver\Query::__construct</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\Query::__construct</span> &mdash; <span class="dc-title">Create a new Query</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-query.construct-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\Driver\Query::__construct</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span> <code class="parameter">$filter</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$queryOptions</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>)</div>

  <p class="para rdfs-comment">
   Constructs a new <span class="classname"><a href="class.mongodb-driver-query.php" class="classname">MongoDB\Driver\Query</a></span>, which is an
   immutable value object that represents a database query. The query may then
   be executed with
   <span class="methodname"><a href="mongodb-driver-manager.executequery.php" class="methodname">MongoDB\Driver\Manager::executeQuery()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-query.construct-parameters">
  <h3 class="title">Параметри</h3>
  <dl>
   
   
    <dt><code class="parameter">filter</code>
    (<span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span>)</dt>
    <dd>
     <p class="para">
      <a href="https://www.mongodb.com/docs/manual/tutorial/query-documents/" class="link external">&raquo;&nbsp;Предикат запиту</a>.
      Порожній предикат відповідає всім документам у колекції.
     </p>
     <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
      <span class="simpara">
       Під час виконання критеріїв запиту MongoDB порівнює типи та значення за
       власними <a href="https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/" class="link external">&raquo;&nbsp;правилами порівняння
       BSON-типів</a>, що відрізняється від правил
       <a href="types.comparisons.php" class="link">порівняння</a> і
       <a href="language.types.type-juggling.php" class="link">перетворення типів</a> в
       PHP. Для вибірки даних спеціальних BSON-типів, критерії запиту повинні
       використовувати відповідний <a href="mongodb.bson.php" class="link">клас BSON</a>
       (напр. <span class="classname"><a href="class.mongodb-bson-objectid.php" class="classname">MongoDB\BSON\ObjectId</a></span> для вибірки
       <a href="https://www.mongodb.com/docs/manual/reference/bson-types/#objectid" class="link external">&raquo;&nbsp;ObjectId</a>).
      </span>
     </p></blockquote>
    </dd>
   

   
    <dt><code class="parameter">queryOptions</code></dt>
    <dd>
     <p class="para">
      <table id="mongodb-driver-query.construct-queryOptions" class="doctable table">
       <caption><strong>queryOptions</strong></caption>
       
        <thead>
         <tr>
          <th>Option</th>
          <th>Type</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>allowDiskUse</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <p class="para">
            Allows MongoDB to use temporary disk files to store data exceeding
            the 100 megabyte system memory limit while processing a blocking
            sort operation.
           </p>
          </td>
         </tr>

         <tr>
          <td>allowPartialResults</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <p class="para">
            For queries against a sharded collection, returns partial results
            from the mongos if some shards are unavailable instead of throwing
            an error.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;partial&quot;</code> option if
            not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>awaitData</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           Use in conjunction with the <code class="literal">&quot;tailable&quot;</code> option to
           block a getMore operation on the cursor temporarily if at the end of
           data rather than returning no data. After a timeout period, the query
           returns as normal.
          </td>
         </tr>

         <tr>
          <td>batchSize</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <p class="para">
            The number of documents to return in the first batch. Defaults to
            101. A batch size of 0 means that the cursor will be established,
            but no documents will be returned in the first batch.
           </p>
           <p class="para">
            In versions of MongoDB before 3.2, where queries use the legacy wire
            protocol OP_QUERY, a batch size of 1 will close the cursor
            irrespective of the number of matched documents.
           </p>
          </td>
         </tr>

         
         <tr>
          <td>collation</td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">
            <a href="https://www.mongodb.com/docs/upcoming/reference/collation/" class="link external">&raquo;&nbsp;Зіставлення</a>
            дозволяє вказувати специфічні для мови правила порівняння рядків,
            як от правила регістру та знаків наголосу. Під час визначення
            зіставлень поле <code class="literal">&quot;locale&quot;</code> є обов&#039;язковим, а
            всі інші поля — ні. Опис полів є на сторінці <a href="https://www.mongodb.com/docs/upcoming/reference/collation/#collation-document" class="link external">&raquo;&nbsp;Документ
            зіставлення</a>.
           </p>
           <p class="para">
            Якщо зіставлення не вказано, то використовується стандартне
            зіставлення для колекції, якщо воно вказано. Якщо ж ні, MongoDB
            використовує просте двійкове порівняння, яке використовувалося в
            попередніх версіях для порівняння рядків.
           </p>
           <p class="para">
            Цей параметр доступний у MongoDB 3.4+, тож його застосування у
            старіших версіях викине виключення під час виконання.
           </p>
          </td>
         </tr>


         <tr>
          <td>comment</td>
          <td><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></td>
          <td>
           <p class="para">
            An arbitrary comment to help trace the operation through the
            database profiler, currentOp output, and logs.
           </p>
           <p class="para">
            The comment can be any valid BSON type for MongoDB 4.4+. Earlier
            server versions only support string values.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$comment&quot;</code> modifier
            if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>exhaust</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <p class="para">
            Stream the data down full blast in multiple &quot;more&quot; packages, on the
            assumption that the client will fully read all data queried. Faster
            when you are pulling a lot of data and know you want to pull it all
            down. Note: the client is not allowed to not read all the data
            unless it closes the connection.
           </p>
           <p class="para">
            This option is not supported by the find command in MongoDB 3.2+ and
            will force the driver to use the legacy wire protocol version (i.e.
            OP_QUERY).
           </p>
          </td>
         </tr>

         <tr>
          <td>explain</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <p class="para">
            If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, the returned <span class="classname"><a href="class.mongodb-driver-cursor.php" class="classname">MongoDB\Driver\Cursor</a></span>
            will contain a single document that describes the process and
            indexes used to return the query.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$explain&quot;</code> modifier
            if not specified.
           </p>
           <p class="para">
            This option is not supported by the find command in MongoDB 3.2+ and
            will only be respected when using the legacy wire protocol version
            (i.e. OP_QUERY). The
            <a href="https://www.mongodb.com/docs/manual/reference/command/explain/" class="link external">&raquo;&nbsp;explain</a>
            command should be used on MongoDB 3.0+.
           </p>
          </td>
         </tr>

         <tr>
          <td>hint</td>
          <td><span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">
            Index specification. Specify either the index name as a string or
            the index key pattern. If specified, then the query system will only
            consider plans using the hinted index.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;hint&quot;</code> option if not
            specified.
           </p>
          </td>
         </tr>

         
         <tr>
          <td>let</td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">
            Карта назв і значень параметрів. Значення мають бути константами
            або закритими виразами, які не посилаються на поля документа. Тоді
            параметри можуть бути доступні як змінні в контексті агрегатного
            виразу (напр. <code class="literal">$$var</code>).
           </p>
           <p class="para">
            Цей параметр доступний у MongoDB 5.0+, тож його застосування у
            старіших версіях викине виключення під час виконання.
           </p>
          </td>
         </tr>


         <tr>
          <td>limit</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <p class="para">
            The maximum number of documents to return. If unspecified, then
            defaults to no limit. A limit of 0 is equivalent to setting no
            limit.
           </p>
          </td>
         </tr>

         <tr>
          <td>max</td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">
            The <em>exclusive</em> upper bound for a specific index.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$max&quot;</code> modifier if
            not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>maxAwaitTimeMS</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <p class="para">
            Positive integer denoting the time limit in milliseconds for the
            server to block a getMore operation if no data is available. This
            option should only be used in conjunction with the
            <code class="literal">&quot;tailable&quot;</code> and <code class="literal">&quot;awaitData&quot;</code>
            options.
           </p>
          </td>
         </tr>

         <tr>
          <td>maxTimeMS</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <p class="para">
            The cumulative time limit in milliseconds for processing operations
            on the cursor. MongoDB aborts the operation at the earliest
            following interrupt point.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$maxTimeMS&quot;</code>
            modifier if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>min</td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">
            The <em>inclusive</em> lower bound for a specific index.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$min&quot;</code> modifier if
            not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>noCursorTimeout</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           Prevents the server from timing out idle cursors after an inactivity
           period (10 minutes).
          </td>
         </tr>

         <tr>
          <td>projection</td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">
            The <a href="https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/" class="link external">&raquo;&nbsp;projection specification</a>
            to determine which fields to include in the returned documents.
           </p>
           <p class="para">
            If you are using the <a href="mongodb.persistence.php#mongodb.persistence.deserialization" class="link">ODM
            functionality</a> to deserialise documents as their original
            PHP class, make sure that you include the
            <span class="property">__pclass</span> field in the projection. This is
            required for the deserialization to work and without it, the
            extension will return (by default) a <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span>
            object instead.
           </p>
          </td>
         </tr>

         <tr>
          <td>readConcern</td>
          <td><span class="classname"><a href="class.mongodb-driver-readconcern.php" class="classname">MongoDB\Driver\ReadConcern</a></span></td>
          <td>
           <p class="para">
            A read concern to apply to the operation. By default, the read
            concern from the
            <a href="mongodb-driver-manager.construct.php#mongodb-driver-manager.construct-uri" class="link">MongoDB
            Connection URI</a> will be used.
           </p>
           <p class="para">
            This option is available in MongoDB 3.2+ and will result in an
            exception at execution time if specified for an older server
            version.
           </p>
          </td>
         </tr>

         <tr>
          <td>returnKey</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <p class="para">
            If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, returns only the index keys in the resulting documents.
            Default value is <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> and the find command does not
            use an index, the returned documents will be empty.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$returnKey&quot;</code>
            modifier if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>showRecordId</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <p class="para">
            Determines whether to return the record identifier for each
            document. If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, adds a top-level <code class="literal">&quot;$recordId&quot;</code>
            field to the returned documents.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$showDiskLoc&quot;</code>
            modifier if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>singleBatch</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           Determines whether to close the cursor after the first batch.
           Defaults to <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
          </td>
         </tr>

         <tr>
          <td>skip</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>Number of documents to skip. Defaults to 0.</td>
         </tr>

         <tr>
          <td>sort</td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></td>
          <td>
           <p class="para">The sort specification for the ordering of the results.</p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$orderby&quot;</code> modifier
            if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>tailable</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>Returns a tailable cursor for a capped collection.</td>
         </tr>

        </tbody>
       
      </table>

     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 errors" id="refsect1-mongodb-driver-query.construct-errors">
  <h3 class="title">Помилки/виключення</h3>
  <ul class="simplelist">
   <li>У разі помилки обробки параметрів
викидається
<span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.php" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span>.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-query.construct-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 2.0.0</td>
       <td>
        <p class="para">
         The <code class="literal">&quot;partial&quot;</code> option was removed. Use the
         <code class="literal">&quot;allowPartialResults&quot;</code> option instead.
        </p>
        <p class="para">
         The <code class="literal">&quot;maxScan&quot;</code> option was removed. Support for this
         option was removed in MongoDB 4.2.
        </p>
        <p class="para">
         The <code class="literal">&quot;modifiers&quot;</code> option was removed. This option was
         used for legacy query modifiers, which are all deprecated.
        </p>
        <p class="para">
         The <code class="literal">&quot;oplogReplay&quot;</code> option was removed. It is ignored
         in MongoDB 4.4 and newer.
        </p>
        <p class="para">
         The <code class="literal">&quot;snapshot&quot;</code> option was removed. Support for this
         option was removed in MongoDB 4.0.
        </p>
        <p class="para">
         A negative value for the <code class="literal">&quot;limit&quot;</code> option no longer
         implies <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for the <code class="literal">&quot;singleBatch&quot;</code> option. To only
         receive a single batch of results, combine a positive
         <code class="literal">&quot;limit&quot;</code> value with the
         <code class="literal">&quot;singleBatch&quot;</code> option.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.14.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;let&quot;</code> option. The
         <code class="literal">&quot;comment&quot;</code> option now accepts any type.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.8.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;allowDiskUse&quot;</code> option.
        </p>
        <p class="para">
         The <code class="literal">&quot;oplogReplay&quot;</code> option is deprecated.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.5.0</td>
       <td>
        <p class="para">
         The <code class="literal">&quot;maxScan&quot;</code> and <code class="literal">&quot;snapshot&quot;</code>
         options are deprecated.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.3.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;maxAwaitTimeMS&quot;</code> option.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.2.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;allowPartialResults&quot;</code>,
         <code class="literal">&quot;collation&quot;</code>, <code class="literal">&quot;comment&quot;</code>,
         <code class="literal">&quot;hint&quot;</code>, <code class="literal">&quot;max&quot;</code>,
         <code class="literal">&quot;maxScan&quot;</code>, <code class="literal">&quot;maxTimeMS&quot;</code>,
         <code class="literal">&quot;min&quot;</code>, <code class="literal">&quot;returnKey&quot;</code>,
         <code class="literal">&quot;showRecordId&quot;</code>, and <code class="literal">&quot;snapshot&quot;</code>
         options.
        </p>
        <p class="para">
         Renamed the <code class="literal">&quot;partial&quot;</code> option to
         <code class="literal">&quot;allowPartialResults&quot;</code>. For backwards compatibility,
         <code class="literal">&quot;partial&quot;</code> will still be read if
         <code class="literal">&quot;allowPartialResults&quot;</code> is not specified.
        </p>
        <p class="para">
         Removed the legacy <code class="literal">&quot;secondaryOk&quot;</code> option, which is
         obsolete. For queries using the legacy wire protocol OP_QUERY, the
         driver will set the <code class="literal">secondaryOk</code> bit as needed in
         accordance with the
         <a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.md" class="link external">&raquo;&nbsp;Server Selection Specification</a>.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.1.0</td>
       <td>Added the <code class="literal">&quot;readConcern&quot;</code> option.</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb-driver-query.construct-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-1">
   <p><strong>Приклад #1 <span class="function"><strong>MongoDB\Driver\Query::__construct()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Select only documents authord by "bjori" with at least 100 views */<br /></span><span style="color: #0000BB">$filter </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">'author' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'bjori'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'views' </span><span style="color: #007700">=&gt; [<br />        </span><span style="color: #DD0000">'$gte' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />    ],<br />];<br /><br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= [<br />    </span><span style="color: #FF8000">/* Only return the following fields in the matching documents */<br />    </span><span style="color: #DD0000">'projection' </span><span style="color: #007700">=&gt; [<br />        </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'article' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />    ],<br />    </span><span style="color: #FF8000">/* Return the documents in descending order of views */<br />    </span><span style="color: #DD0000">'sort' </span><span style="color: #007700">=&gt; [<br />        </span><span style="color: #DD0000">'views' </span><span style="color: #007700">=&gt; -</span><span style="color: #0000BB">1<br />    </span><span style="color: #007700">],<br />];<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoDB\Driver\Query</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$manager </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoDB\Driver\Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost:27017'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$readPreference </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoDB\Driver\ReadPreference</span><span style="color: #007700">(</span><span style="color: #0000BB">MongoDB\Driver\ReadPreference</span><span style="color: #007700">::</span><span style="color: #0000BB">PRIMARY</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #DD0000">'databaseName.collectionName'</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">, [</span><span style="color: #DD0000">'readPreference' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$readPreference</span><span style="color: #007700">]);<br /><br />foreach(</span><span style="color: #0000BB">$cursor </span><span style="color: #007700">as </span><span style="color: #0000BB">$document</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongodb-driver-query.construct-seealso">
  <h3 class="title">Прогляньте також</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="mongodb-driver-manager.executequery.php" class="methodname" rel="rdfs-seeAlso">MongoDB\Driver\Manager::executeQuery()</a> - Execute a database query</span></li>
   <li><span class="classname"><a href="class.mongodb-driver-cursor.php" class="classname">MongoDB\Driver\Cursor</a></span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>