<!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>Sets a server-side timeout for this query</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.limit.html">MongoCursor::limit</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.next.html">MongoCursor::next</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocursor.maxtimems" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCursor::maxTimeMS</h1> <p class="verinfo">(PECL mongo >=1.5.0)</p><p class="refpurpose"><span class="refname">MongoCursor::maxTimeMS</span> — <span class="dc-title">Sets a server-side timeout for this query</span></p> </div> <div class="refsect1 description" id="refsect1-mongocursor.maxtimems-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.mongocursor.html" class="type MongoCursor">MongoCursor</a></span> <span class="methodname"><strong>MongoCursor::maxTimeMS</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$ms</code></span> )</div> <p class="para rdfs-comment"> Specifies a cumulative time limit in milliseconds to be allowed by the server for processing operations on the cursor. </p> </div> <div class="refsect1 parameters" id="refsect1-mongocursor.maxtimems-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">ms</code> </dt> <dd> <p class="para"> Specifies a cumulative time limit in milliseconds to be allowed by the server for processing operations on the cursor. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-mongocursor.maxtimems-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> This cursor. </p> </div> <div class="refsect1 errors" id="refsect1-mongocursor.maxtimems-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Throws <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a> if this cursor has started iterating. </p> <p class="para"> Causes methods that fetch results to throw a <a href="class.mongoexecutiontimeoutexception.html" class="classname">MongoExecutionTimeoutException</a> if the query takes longer than the specified number of milliseconds in processing time. </p> </div> <div class="refsect1 examples" id="refsect1-mongocursor.maxtimems-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1625"> <p><strong>Example #1 <span class="methodname"><strong>MongoCursor::maxTimeMS()</strong></span> example</strong></p> <div class="example-contents"><p> In the following example, the server will abort the query if the cursor requires more than two seconds in processing time to return its results. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">maxTimeMS</span><span style="color: #007700">(</span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br /><br />try {<br /> </span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">iterator_to_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">);<br />} catch (</span><span style="color: #0000BB">MongoExecutionTimeoutException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"query took too long!"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 notes" id="refsect1-mongocursor.maxtimems-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> Unlike <span class="methodname"><a href="mongocursor.timeout.html" class="methodname">MongoCursor::timeout()</a></span>, which specifies a client-side timeout, <span class="methodname"><strong>MongoCursor::maxTimeMS()</strong></span> can be used to cause the MongoDB server to abort long-running queries. This timeout is cumulative for the lifetime of the cursor (i.e. each batch will contribute to this time limit). The timeout only considers processing time; idle time is not considered. </p> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.limit.html">MongoCursor::limit</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.next.html">MongoCursor::next</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>