Skip to content

Commit a995a88

Browse files
committed
Improve Command Cursor suport
1 parent eaaf42b commit a995a88

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

examples/write.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@
6262
echo "Distinct countries:\n";
6363
var_dump($result);
6464

65-
$aggregate = $collection->aggregate(array(array('$project' => array("name" => 1, "_id" => 0))), array("useCursor" => false));
65+
echo "aggregate\n";
66+
$aggregate = $collection->aggregate(array(array('$project' => array("name" => 1, "_id" => 0))), array("useCursor" => true, "batchSize" => 2));
6667
printf("Should be 3 different people\n");
67-
var_dump($aggregate);
68+
foreach($aggregate as $person) {
69+
var_dump($person);
70+
}
6871

6972
$result = $collection->updateMany(
7073
array("citizen" => "Iceland"),

src/MongoDB/Collection.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,16 @@ function aggregate(array $pipeline, array $options = array()) { /* {{{ */
296296
"pipeline" => $pipeline,
297297
) + $options;
298298

299-
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
300-
if ($doc["ok"]) {
301-
return $doc["result"];
299+
$result = $this->_runCommand($this->dbname, $cmd);
300+
$doc = $result->getResponseDocument();
301+
if (isset($cmd["cursor"]) && $cmd["cursor"]) {
302+
return $result;
303+
} else {
304+
if ($doc["ok"]) {
305+
return new \ArrayIterator($doc["result"]);
306+
}
302307
}
308+
303309
throw $this->_generateCommandException($doc);
304310
} /* }}} */
305311
function getAggregateOptions() { /* {{{ */
@@ -358,7 +364,7 @@ protected function _generateCommandException($doc) { /* {{{ */
358364
return new Exception($doc["errmsg"]);
359365
}
360366
var_dump($doc);
361-
return new Exception("FIXME: Unknown error");
367+
return new \Exception("FIXME: Unknown error");
362368
} /* }}} */
363369
protected function _runCommand($dbname, array $cmd, ReadPreference $rp = null) { /* {{{ */
364370
//var_dump(\BSON\toJSON(\BSON\fromArray($cmd)));

0 commit comments

Comments
 (0)