<!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 whether this cursor will wait for a while for a tailable cursor to return more data</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.addoption.html">MongoCursor::addOption</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.batchsize.html">MongoCursor::batchSize</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.awaitdata" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCursor::awaitData</h1> <p class="verinfo">(PECL mongo >=1.2.11)</p><p class="refpurpose"><span class="refname">MongoCursor::awaitData</span> — <span class="dc-title">Sets whether this cursor will wait for a while for a tailable cursor to return more data</span></p> </div> <div class="refsect1 description" id="refsect1-mongocursor.awaitdata-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::awaitData</strong></span> ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$wait</code><span class="initializer"> = true</span></span> ] )</div> <p class="para rdfs-comment"> This method is to be used with tailable cursors. If we are at the end of the data, block for a while rather than returning no data. After a timeout period, we do return as normal. </p> </div> <div class="refsect1 parameters" id="refsect1-mongocursor.awaitdata-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"> <em><code class="parameter">wait</code></em> </span> <dd> <p class="para"> If the cursor should wait for more data to become available. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocursor.awaitdata-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns this cursor. </p> </div> <div class="refsect1 errors" id="refsect1-mongocursor.awaitdata-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> </div> <div class="refsect1 examples" id="refsect1-mongocursor.awaitdata-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1476"> <p><strong>Example #1 <span class="function"><strong>MongoCursor::awaitData()</strong></span> example</strong></p> <div class="example-contents"><p> In this example we tail the "oplog" and instead of sleeping during every iteration, we set the <span class="function"><strong>MongoCursor::awaitData()</strong></span> option. <span class="function"><a href="mongocursor.hasnext.html" class="function">MongoCursor::hasNext()</a></span> will now block until there is more data available. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<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://localhost:13000'</span><span style="color: #007700">, array( </span><span style="color: #DD0000">'replSet' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'seta' </span><span style="color: #007700">) );<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">local</span><span style="color: #007700">-></span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">( </span><span style="color: #DD0000">'oplog.rs' </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">( array( </span><span style="color: #DD0000">'ns' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'demo.article'</span><span style="color: #007700">, </span><span style="color: #DD0000">'op' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'i' </span><span style="color: #007700">) );<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">tailable</span><span style="color: #007700">( </span><span style="color: #0000BB">true </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">awaitData</span><span style="color: #007700">( </span><span style="color: #0000BB">true </span><span style="color: #007700">);<br /><br />while (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">hasNext</span><span style="color: #007700">()) {<br /> </span><span style="color: #FF8000">// we've read all the results, exit<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">dead</span><span style="color: #007700">()) {<br /> break;<br /> }<br /> } else {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">getNext</span><span style="color: #007700">() );<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongocursor.awaitdata-seealso"> <h3 class="title">See Also</h3> <p class="para"> MongoDB core docs on <a href="http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/" class="link external">» tailable cursors</a>. </p> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="mongocursor.tailable.html" class="function" rel="rdfs-seeAlso">MongoCursor::tailable()</a> - Sets whether this cursor will be left open after fetching the last results</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.addoption.html">MongoCursor::addOption</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.batchsize.html">MongoCursor::batchSize</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>