<?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 => 'zh',
  ),
  '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="simpara">
   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">
      The <a href="https://www.mongodb.com/docs/manual/tutorial/query-documents/" class="link external">&raquo;&nbsp;query predicate</a>.
      An empty predicate will match all documents in the collection.
     </p>
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <span class="simpara">
       When evaluating query criteria, MongoDB compares types and values according to its own <a href="https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/" class="link external">&raquo;&nbsp;comparison rules for BSON types</a>, which differs from PHP&#039;s <a href="types.comparisons.php" class="link">comparison</a> and <a href="language.types.type-juggling.php" class="link">type juggling</a> rules. When matching a special BSON type the query criteria should use the respective <a href="mongodb.bson.php" class="link">BSON class</a> (e.g. use <span class="classname"><a href="class.mongodb-bson-objectid.php" class="classname">MongoDB\BSON\ObjectId</a></span> to match an <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>
           <span class="simpara">
            Allows MongoDB to use temporary disk files to store data exceeding
            the 100 megabyte system memory limit while processing a blocking
            sort operation.
           </span>
          </td>
         </tr>

         <tr>
          <td>allowPartialResults</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <span class="simpara">
            For queries against a sharded collection, returns partial results
            from the mongos if some shards are unavailable instead of throwing
            an error.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;partial&quot;</code> option if
            not specified.
           </span>
          </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>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            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.
           </span>
          </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;Collation</a> allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the <code class="literal">&quot;locale&quot;</code> field is mandatory; all other collation fields are optional. For descriptions of the fields, see <a href="https://www.mongodb.com/docs/upcoming/reference/collation/#collation-document" class="link external">&raquo;&nbsp;Collation Document</a>.
           </p>
           <p class="para">
            If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.
           </p>
           <p class="para">
            This option is available in MongoDB 3.4+ and will result in an exception at execution time if specified for an older server version.
           </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>
           <span class="simpara">
            An arbitrary comment to help trace the operation through the
            database profiler, currentOp output, and logs.
           </span>
           <span class="simpara">
            The comment can be any valid BSON type for MongoDB 4.4+. Earlier
            server versions only support string values.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$comment&quot;</code> modifier
            if not specified.
           </span>
          </td>
         </tr>

         <tr>
          <td>exhaust</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            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).
           </span>
          </td>
         </tr>

         <tr>
          <td>explain</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$explain&quot;</code> modifier
            if not specified.
           </span>
           <span class="simpara">
            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+.
           </span>
          </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>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;hint&quot;</code> option if not
            specified.
           </span>
          </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">
            Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g. <code class="literal">$$var</code>).
           </p>
           <p class="para">
            This option is available in MongoDB 5.0+ and will result in an exception at execution time if specified for an older server version.
           </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>
           <span class="simpara">
            The maximum number of documents to return. If unspecified, then
            defaults to no limit. A limit of 0 is equivalent to setting no
            limit.
           </span>
          </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>
           <span class="simpara">
            The <em>exclusive</em> upper bound for a specific index.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$max&quot;</code> modifier if
            not specified.
           </span>
          </td>
         </tr>

         <tr>
          <td>maxAwaitTimeMS</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <span class="simpara">
            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.
           </span>
          </td>
         </tr>

         <tr>
          <td>maxTimeMS</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <span class="simpara">
            The cumulative time limit in milliseconds for processing operations
            on the cursor. MongoDB aborts the operation at the earliest
            following interrupt point.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$maxTimeMS&quot;</code>
            modifier if not specified.
           </span>
          </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>
           <span class="simpara">
            The <em>inclusive</em> lower bound for a specific index.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$min&quot;</code> modifier if
            not specified.
           </span>
          </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>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            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.
           </span>
          </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>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            This option is available in MongoDB 3.2+ and will result in an
            exception at execution time if specified for an older server
            version.
           </span>
          </td>
         </tr>

         <tr>
          <td>returnKey</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$returnKey&quot;</code>
            modifier if not specified.
           </span>
          </td>
         </tr>

         <tr>
          <td>showRecordId</td>
          <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
          <td>
           <span class="simpara">
            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.
           </span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$showDiskLoc&quot;</code>
            modifier if not specified.
           </span>
          </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>
           <span class="simpara">The sort specification for the ordering of the results.</span>
           <span class="simpara">
            Falls back to the deprecated <code class="literal">&quot;$orderby&quot;</code> modifier
            if not specified.
           </span>
          </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>Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.php" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> on argument parsing errors.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-query.construct-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>PECL mongodb 2.0.0</td>
      <td>
       <span class="simpara">
        The <code class="literal">&quot;partial&quot;</code> option was removed. Use the
        <code class="literal">&quot;allowPartialResults&quot;</code> option instead.
       </span>
       <span class="simpara">
        The <code class="literal">&quot;maxScan&quot;</code> option was removed. Support for this
        option was removed in MongoDB 4.2.
       </span>
       <span class="simpara">
        The <code class="literal">&quot;modifiers&quot;</code> option was removed. This option was
        used for legacy query modifiers, which are all deprecated.
       </span>
       <span class="simpara">
        The <code class="literal">&quot;oplogReplay&quot;</code> option was removed. It is ignored
        in MongoDB 4.4 and newer.
       </span>
       <span class="simpara">
        The <code class="literal">&quot;snapshot&quot;</code> option was removed. Support for this
        option was removed in MongoDB 4.0.
       </span>
       <span class="simpara">
        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.
       </span>
      </td>
     </tr>

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

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

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

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

     <tr>
      <td>PECL mongodb 1.2.0</td>
      <td>
       <span class="simpara">
        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.
       </span>
       <span class="simpara">
        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.
       </span>
       <span class="simpara">
        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>.
       </span>
      </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>

 </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); ?>