<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Set the read preference for this command</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocommandcursor.rewind.html">MongoCommandCursor::rewind</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocommandcursor.timeout.html">MongoCommandCursor::timeout</a></div> <div class="up"><a href="class.mongocommandcursor.html">MongoCommandCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocommandcursor.setreadpreference" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCommandCursor::setReadPreference</h1> <p class="verinfo">(PECL mongo >=1.6.0)</p><p class="refpurpose"><span class="refname">MongoCommandCursor::setReadPreference</span> — <span class="dc-title">Set the read preference for this command</span></p> </div> <div class="refsect1 description" id="refsect1-mongocommandcursor.setreadpreference-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.mongocommandcursor.html" class="type MongoCommandCursor">MongoCommandCursor</a></span> <span class="methodname"><strong>MongoCommandCursor::setReadPreference</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$read_preference</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$tags</code></span> ] )</div> </div> <div class="refsect1 parameters" id="refsect1-mongocommandcursor.setreadpreference-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">read_preference</code></dt> <dd> <p class="para">The read preference mode: <strong><code>MongoClient::RP_PRIMARY</code></strong>, <strong><code>MongoClient::RP_PRIMARY_PREFERRED</code></strong>, <strong><code>MongoClient::RP_SECONDARY</code></strong>, <strong><code>MongoClient::RP_SECONDARY_PREFERRED</code></strong>, or <strong><code>MongoClient::RP_NEAREST</code></strong>.</p></dd> <dt> <code class="parameter">tags</code></dt> <dd> <p class="para">An array of zero or more tag sets, where each tag set is itself an array of criteria used to match tags on replica set members.</p></dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-mongocommandcursor.setreadpreference-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns this cursor. </p> </div> <div class="refsect1 errors" id="refsect1-mongocommandcursor.setreadpreference-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para">Emits <strong><code>E_WARNING</code></strong> if either parameter is invalid, or if one or more tag sets are provided with the <strong><code>MongoClient::RP_PRIMARY</code></strong> read preference mode.</p> </div> <div class="refsect1 examples" id="refsect1-mongocommandcursor.setreadpreference-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1639"> <p><strong>Example #1 <span class="methodname"><strong>MongoCommandCursor::setReadPreference()</strong></span> tag set array syntax example </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://rs1.example.com:27017'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'replicaSet' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'myReplSetName'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">, </span><span style="color: #DD0000">'people'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">aggregateCursor</span><span style="color: #007700">( [<br /> [ </span><span style="color: #DD0000">'$group' </span><span style="color: #007700">=> [ </span><span style="color: #DD0000">'_id' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'$name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'points' </span><span style="color: #007700">=> [ </span><span style="color: #DD0000">'$sum' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'$points' </span><span style="color: #007700">] ] ],<br /> [ </span><span style="color: #DD0000">'$sort' </span><span style="color: #007700">=> [ </span><span style="color: #DD0000">'points' </span><span style="color: #007700">=> -</span><span style="color: #0000BB">1 </span><span style="color: #007700">] ],<br />] );<br /><br /></span><span style="color: #FF8000">// Prefer the nearest server in the "east" data center also used for reporting,<br />// but fall back to a server in the "west" data center<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">setReadPreference</span><span style="color: #007700">(</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">::</span><span style="color: #0000BB">RP_NEAREST</span><span style="color: #007700">, [<br /> [ </span><span style="color: #DD0000">'dc' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'east'</span><span style="color: #007700">, </span><span style="color: #DD0000">'use' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'reporting' </span><span style="color: #007700">],<br /> [ </span><span style="color: #DD0000">'dc' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'west' </span><span style="color: #007700">],<br />] );<br /><br />foreach (</span><span style="color: #0000BB">$cursor </span><span style="color: #007700">as </span><span style="color: #0000BB">$person</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// ...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// If the read preference is changed, it will be used the next time the cursor<br />// is rewound and the command is re-executed.<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">setReadPreference</span><span style="color: #007700">(</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">::</span><span style="color: #0000BB">RP_PRIMARY</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">$person</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// ...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongocommandcursor.setreadpreference-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member">The <a href="mongo.readpreferences.html" class="link">read preferences</a> documentation.</li> <li class="member"><span class="function"><a href="mongocommandcursor.getreadpreference.html" class="function" rel="rdfs-seeAlso">MongoCommandCursor::getReadPreference()</a> - Get the read preference for this command</span></li> <li class="member"><span class="function"><a href="mongocursorinterface.setreadpreference.html" class="function" rel="rdfs-seeAlso">MongoCursorInterface::setReadPreference()</a> - Set the read preference for this query</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocommandcursor.rewind.html">MongoCommandCursor::rewind</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocommandcursor.timeout.html">MongoCommandCursor::timeout</a></div> <div class="up"><a href="class.mongocommandcursor.html">MongoCommandCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>