<?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-readpreference.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'mongodb-driver-readpreference.construct.php',
    1 => 'MongoDB\\Driver\\ReadPreference::__construct',
    2 => 'Create a new ReadPreference',
  ),
  'up' => 
  array (
    0 => 'class.mongodb-driver-readpreference.php',
    1 => 'MongoDB\\Driver\\ReadPreference',
  ),
  'prev' => 
  array (
    0 => 'mongodb-driver-readpreference.bsonserialize.php',
    1 => 'MongoDB\\Driver\\ReadPreference::bsonSerialize',
  ),
  'next' => 
  array (
    0 => 'mongodb-driver-readpreference.gethedge.php',
    1 => 'MongoDB\\Driver\\ReadPreference::getHedge',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mongodb/mongodb/driver/readpreference/construct.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

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

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-readpreference.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\ReadPreference::__construct</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$mode</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">$tagSets</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></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">$options</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-readpreference.php" class="classname">MongoDB\Driver\ReadPreference</a></span>, which
   is an immutable value object.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-readpreference.construct-parameters">
  <h3 class="title">パラメータ</h3>
  <dl>
   
    <dt><code class="parameter">mode</code></dt>
    <dd>
     <p class="para">
      <table class="doctable table">
       <caption><strong>Read preference mode</strong></caption>
       
        <thead>
         <tr>
          <th>Value</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><code class="literal">&quot;primary&quot;</code></td>
          <td>
           <span class="simpara">
            All operations read from the current replica set primary. This is
            the default read preference for MongoDB.
           </span>
          </td>
         </tr>

         <tr>
          <td><code class="literal">&quot;primaryPreferred&quot;</code></td>
          <td>
           <span class="simpara">
            In most situations, operations read from the primary but if it is
            unavailable, operations read from secondary members.
           </span>
          </td>
         </tr>

         <tr>
          <td><code class="literal">&quot;secondary&quot;</code></td>
          <td>
           <span class="simpara">
            All operations read from the secondary members of the replica set.
           </span>
          </td>
         </tr>

         <tr>
          <td><code class="literal">&quot;secondaryPreferred&quot;</code></td>
          <td>
           <span class="simpara">
            In most situations, operations read from secondary members but if no
            secondary members are available, operations read from the primary.
           </span>
          </td>
         </tr>

         <tr>
          <td><code class="literal">&quot;nearest&quot;</code></td>
          <td>
           <span class="simpara">
            Operations read from member of the replica set with the least
            network latency, irrespective of the member&#039;s type.
           </span>
          </td>
         </tr>

        </tbody>
       
      </table>

     </p>
    </dd>
   
   
    <dt><code class="parameter">tagSets</code></dt>
    <dd>
     <span class="simpara">
      Tag sets allow you to target read operations to specific members of a
      replica set. This parameter should be an array of associative arrays, each
      of which contain zero or more key/value pairs. When selecting a server for
      a read operation, the driver attempt to select a node having all tags in a
      set (i.e. the associative array of key/value pairs). If selection fails,
      the driver will attempt subsequent sets. An empty tag set
      (<code class="literal">array()</code>) will match any node and may be used as a
      fallback.
     </span>
     <span class="simpara">
      Tags are not compatible with the <code class="literal">&quot;primary&quot;</code> mode and,
      in general, only apply when selecting a secondary member of a set for a
      read operation. However, the <code class="literal">&quot;nearest&quot;</code> mode, when
      combined with a tag set, selects the matching member with the lowest
      network latency. This member may be a primary or secondary.
     </span>
    </dd>
   
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <p class="para">
      <table class="doctable table">
       <caption><strong>options</strong></caption>
       
        <thead>
         <tr>
          <th>Option</th>
          <th>Type</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>hedge</td>
          <td><span class="type"><span class="type"><a href="language.types.object.php" class="type object">object</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span></span></td>
          <td>
           <span class="simpara">Specifies whether to use <a href="https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router/#mongos-hedged-reads" class="link external">&raquo;&nbsp;hedged reads</a>, which are supported by MongoDB 4.4+ for sharded queries.</span>
           <span class="simpara">
            Server hedged reads are available for all non-primary read preferences
            and are enabled by default when using the <code class="literal">&quot;nearest&quot;</code>
            mode. This option allows explicitly enabling server hedged reads for
            non-primary read preferences by specifying
            <code class="literal">[&#039;enabled&#039; =&gt; true]</code>, or explicitly disabling
            server hedged reads for the <code class="literal">&quot;nearest&quot;</code> read
            preference by specifying <code class="literal">[&#039;enabled&#039; =&gt; false]</code>.
           </span>
          </td>
         </tr>

         <tr>
          <td>maxStalenessSeconds</td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <span class="simpara">
            Specifies a maximum replication lag, or &quot;staleness&quot;, for reads from
            secondaries. When a secondary&#039;s estimated staleness exceeds
            this value, the driver stops using it for read operations.
           </span>
           <span class="simpara">
            If specified, the max staleness must be a signed 32-bit integer
            greater than or equal to
            <strong><code><a href="class.mongodb-driver-readpreference.php#mongodb-driver-readpreference.constants.smallest-max-staleness-seconds">MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS</a></code></strong>.
           </span>
           <span class="simpara">
            Defaults to
            <strong><code><a href="class.mongodb-driver-readpreference.php#mongodb-driver-readpreference.constants.no-max-staleness">MongoDB\Driver\ReadPreference::NO_MAX_STALENESS</a></code></strong>,
            which means that the driver will not consider a secondary&#039;s lag
            when choosing where to direct a read operation.
           </span>
           <span class="simpara">
            This option is not compatible with the <code class="literal">&quot;primary&quot;</code>
            mode. Specifying a max staleness also requires all MongoDB instances
            in the deployment to be using MongoDB 3.4+. An exception will be
            thrown at execution time if any MongoDB instances in the deployment
            are of an older server version.
           </span>
          </td>
         </tr>

        </tbody>
       
      </table>

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


 <div class="refsect1 errors" id="refsect1-mongodb-driver-readpreference.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>
   <li>Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.php" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> if <code class="parameter">mode</code> is invalid.</li>
   <li>Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.php" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> if <code class="parameter">tagSets</code> is provided for a primary read preference or is malformed (i.e. not an array of zero or more documents).</li>
   <li>Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.php" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> if the <code class="literal">&quot;maxStalenessSeconds&quot;</code> option is provided for a primary read preference or is out of range.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-readpreference.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>
       Passing an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> for the <code class="parameter">mode</code> argument
       is no longer supported.
      </td>
     </tr>

     <tr>
      <td>PECL mongodb 1.20.0</td>
      <td>
       Passing an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> for the <code class="parameter">mode</code> argument
       is <em>DEPRECATED</em>.
      </td>
     </tr>

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

     <tr>
      <td>PECL mongodb 1.3.0</td>
      <td>
       <span class="simpara">
        The <code class="parameter">mode</code> argument now accepts a string value,
        which is consistent with the <code class="literal">&quot;readPreference&quot;</code> URI
        option for <span class="function"><a href="mongodb-driver-manager.construct.php" class="function">MongoDB\Driver\Manager::__construct()</a></span>.
       </span>
      </td>
     </tr>

     <tr>
      <td>PECL mongodb 1.2.0</td>
      <td>
       <span class="simpara">
        Added a third <code class="parameter">options</code> argument, which supports
        the <code class="literal">&quot;maxStalenessSeconds&quot;</code> option.
       </span>
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-mongodb-driver-readpreference.construct-examples">
  <h3 class="title">例</h3>
  <div class="example" id="example-1">
   <p><strong>例1 <span class="function"><strong>MongoDB\Driver\ReadPreference::__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 /><br /></span><span style="color: #FF8000">/* Prefer a secondary node but fall back to a primary. */<br /></span><span style="color: #0000BB">var_dump</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">SECONDARY_PREFERRED</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">/* Prefer a node in the New York data center with lowest latency. */<br /></span><span style="color: #0000BB">var_dump</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">NEAREST</span><span style="color: #007700">, [[</span><span style="color: #DD0000">'dc' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'ny'</span><span style="color: #007700">]]));<br /><br /></span><span style="color: #FF8000">/* Require a secondary node whose replication lag is within two minutes of the primary */<br /></span><span style="color: #0000BB">var_dump</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">SECONDARY</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, [</span><span style="color: #DD0000">'maxStalenessSeconds' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">120</span><span style="color: #007700">]));<br /><br /></span><span style="color: #FF8000">/* Explicitly enable server hedged reads */<br /></span><span style="color: #0000BB">var_dump</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">SECONDARY</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, [</span><span style="color: #DD0000">'hedge' </span><span style="color: #007700">=&gt; [</span><span style="color: #DD0000">'enabled' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">true</span><span style="color: #007700">]]));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">object(MongoDB\Driver\ReadPreference)#1 (1) {
  [&quot;mode&quot;]=&gt;
  string(18) &quot;secondaryPreferred&quot;
}
object(MongoDB\Driver\ReadPreference)#1 (2) {
  [&quot;mode&quot;]=&gt;
  string(7) &quot;nearest&quot;
  [&quot;tags&quot;]=&gt;
  array(1) {
    [0]=&gt;
    object(stdClass)#2 (1) {
      [&quot;dc&quot;]=&gt;
      string(2) &quot;ny&quot;
    }
  }
}
object(MongoDB\Driver\ReadPreference)#1 (2) {
  [&quot;mode&quot;]=&gt;
  string(9) &quot;secondary&quot;
  [&quot;maxStalenessSeconds&quot;]=&gt;
  int(120)
}
object(MongoDB\Driver\ReadPreference)#1 (2) {
  [&quot;mode&quot;]=&gt;
  string(9) &quot;secondary&quot;
  [&quot;hedge&quot;]=&gt;
  object(stdClass)#1 (1) {
    [&quot;enabled&quot;]=&gt;
    bool(true)
  }
}</pre>
</div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-mongodb-driver-readpreference.construct-seealso">
  <h3 class="title">参考</h3>
  <ul class="simplelist">
   <li><a href="https://www.mongodb.com/docs/manual/core/read-preference/" class="link external">&raquo;&nbsp;Read Preference reference</a></li>
  </ul>
 </div>


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