<!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>Create a new command cursor from an existing command response document</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocommandcursor.construct.html">MongoCommandCursor::__construct</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocommandcursor.current.html">MongoCommandCursor::current</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.createfromdocument" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCommandCursor::createFromDocument</h1> <p class="verinfo">(PECL mongo >=1.5.0)</p><p class="refpurpose"><span class="refname">MongoCommandCursor::createFromDocument</span> — <span class="dc-title">Create a new command cursor from an existing command response document</span></p> </div> <div class="refsect1 description" id="refsect1-mongocommandcursor.createfromdocument-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="modifier">static</span> <span class="type"><a href="class.mongocommandcursor.html" class="type MongoCommandCursor">MongoCommandCursor</a></span> <span class="methodname"><strong>MongoCommandCursor::createFromDocument</strong></span> ( <span class="methodparam"><span class="type"><a href="class.mongoclient.html" class="type MongoClient">MongoClient</a></span> <code class="parameter">$connection</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$document</code></span> )</div> <p class="para rdfs-comment"> Use this method if you have a raw command result with cursor information in it. Note that cursors created with this method cannot be iterated multiple times, as they will lack the original command necessary for re-execution. </p> </div> <div class="refsect1 parameters" id="refsect1-mongocommandcursor.createfromdocument-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">connection</code> </dt> <dd> <p class="para"> Database connection. </p> </dd> <dt> <code class="parameter">hash</code> </dt> <dd> <p class="para"> The connection hash, as obtained through the third by-reference argument to <span class="methodname"><a href="mongodb.command.html" class="methodname">MongoDB::command()</a></span>. </p> </dd> <dt> <code class="parameter">document</code> </dt> <dd> <p class="para"> Document with cursor information in it. This document needs to contain the <em>id</em>, <em>ns</em> and <em>firstBatch</em> fields. Such a document is obtained by calling the <span class="methodname"><a href="mongodb.command.html" class="methodname">MongoDB::command()</a></span> with appropriate arguments to return a cursor, and not just an inline result. See the example below. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocommandcursor.createfromdocument-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the new cursor. </p> </div> <div class="refsect1 examples" id="refsect1-mongocommandcursor.createfromdocument-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1635"> <p><strong>Example #1 <span class="function"><strong>MongoCommandCursor::createFromDocument()</strong></span></strong></p> <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">;<br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">demo</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Define the aggregation pipeline<br /></span><span style="color: #0000BB">$pipeline </span><span style="color: #007700">= [<br /> [ </span><span style="color: #DD0000">'$group' </span><span style="color: #007700">=> [<br /> </span><span style="color: #DD0000">'_id' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'$country_code'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'timezones' </span><span style="color: #007700">=> [ </span><span style="color: #DD0000">'$addToSet' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'$timezone' </span><span style="color: #007700">]<br /> ] ],<br /> [ </span><span style="color: #DD0000">'$sort' </span><span style="color: #007700">=> [ </span><span style="color: #DD0000">'_id' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1 </span><span style="color: #007700">] ],<br />];<br /><br /></span><span style="color: #FF8000">// Execute the command. The "cursor" option instructs the server to return<br />// cursor information in the response instead of inline results.<br /></span><span style="color: #0000BB">$r </span><span style="color: #007700">= </span><span style="color: #0000BB">$d</span><span style="color: #007700">-></span><span style="color: #0000BB">command</span><span style="color: #007700">(<br /> [<br /> </span><span style="color: #DD0000">'aggregate' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'cities'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'pipeline' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$pipeline</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'cursor' </span><span style="color: #007700">=> [ </span><span style="color: #DD0000">'batchSize' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1 </span><span style="color: #007700">],<br /> ],<br /> </span><span style="color: #0000BB">null</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">$hash<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Show result and hash<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">$r</span><span style="color: #007700">, </span><span style="color: #0000BB">$hash </span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Construct the command cursor<br /></span><span style="color: #0000BB">$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">MongoCommandCursor</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDocument</span><span style="color: #007700">( </span><span style="color: #0000BB">$m</span><span style="color: #007700">, </span><span style="color: #0000BB">$hash</span><span style="color: #007700">, </span><span style="color: #0000BB">$r </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(2) { ["cursor"]=> array(3) { ["id"]=> object(MongoInt64)#5 (1) { ["value"]=> string(12) "392143983421" } ["ns"]=> string(11) "demo.cities" ["firstBatch"]=> array(1) { [0]=> array(2) { ["_id"]=> string(2) "AD" ["timezones"]=> array(1) { [0]=> string(14) "Europe/Andorra" } } } } ["ok"]=> float(1) } string(25) "localhost:27017;-;.;17617" </pre></div> </div> <div class="example-contents"><p> As you can see, the returned cursor information has the <em>id</em>, <em>ns</em> and <em>firstBatch</em> fields. </p></div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongocommandcursor.createfromdocument-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"><span class="function"><a href="mongocommandcursor.construct.html" class="function" rel="rdfs-seeAlso">MongoCommandCursor::__construct()</a> - Create a new command cursor</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.construct.html">MongoCommandCursor::__construct</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocommandcursor.current.html">MongoCommandCursor::current</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>