Skip to content

Commit 3d58b4b

Browse files
authored
Merge pull request #2024 from fsotomsk/master
Add cursor support
2 parents 4d8fb95 + 0aff70a commit 3d58b4b

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
use Illuminate\Database\Query\Expression;
99
use Illuminate\Support\Arr;
1010
use Illuminate\Support\Collection;
11+
use Illuminate\Support\LazyCollection;
1112
use Illuminate\Support\Str;
1213
use Jenssegers\Mongodb\Connection;
1314
use MongoCollection;
1415
use MongoDB\BSON\Binary;
1516
use MongoDB\BSON\ObjectID;
1617
use MongoDB\BSON\Regex;
1718
use MongoDB\BSON\UTCDateTime;
19+
use RuntimeException;
1820

21+
/**
22+
* Class Builder
23+
* @package Jenssegers\Mongodb\Query
24+
*/
1925
class Builder extends BaseBuilder
2026
{
2127
/**
@@ -209,12 +215,25 @@ public function get($columns = [])
209215
return $this->getFresh($columns);
210216
}
211217

218+
/**
219+
* @inheritdoc
220+
*/
221+
public function cursor($columns = [])
222+
{
223+
$result = $this->getFresh($columns, true);
224+
if ($result instanceof LazyCollection) {
225+
return $result;
226+
}
227+
throw new RuntimeException("Query not compatible with cursor");
228+
}
229+
212230
/**
213231
* Execute the query as a fresh "select" statement.
214232
* @param array $columns
215-
* @return array|static[]|Collection
233+
* @param bool $returnLazy
234+
* @return array|static[]|Collection|LazyCollection
216235
*/
217-
public function getFresh($columns = [])
236+
public function getFresh($columns = [], $returnLazy = false)
218237
{
219238
// If no columns have been specified for the select statement, we will set them
220239
// here to either the passed columns, or the standard default of retrieving
@@ -402,6 +421,14 @@ public function getFresh($columns = [])
402421
// Execute query and get MongoCursor
403422
$cursor = $this->collection->find($wheres, $options);
404423

424+
if ($returnLazy) {
425+
return LazyCollection::make(function () use ($cursor) {
426+
foreach ($cursor as $item) {
427+
yield $item;
428+
}
429+
});
430+
}
431+
405432
// Return results as an array with numeric keys
406433
$results = iterator_to_array($cursor, false);
407434
return $this->useCollections ? new Collection($results) : $results;

tests/QueryBuilderTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Illuminate\Support\Facades\Date;
55
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\LazyCollection;
67
use Jenssegers\Mongodb\Collection;
78
use Jenssegers\Mongodb\Query\Builder;
89
use MongoDB\BSON\ObjectId;
@@ -759,4 +760,21 @@ public function testHintOptions()
759760
$this->assertEquals('spork', $results[1]['name']);
760761
$this->assertEquals('fork', $results[0]['name']);
761762
}
763+
764+
public function testCursor()
765+
{
766+
$data = [
767+
['name' => 'fork', 'tags' => ['sharp', 'pointy']],
768+
['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']],
769+
['name' => 'spoon', 'tags' => ['round', 'bowl']],
770+
];
771+
DB::collection('items')->insert($data);
772+
773+
$results = DB::collection('items')->orderBy('_id', 'asc')->cursor();
774+
775+
$this->assertInstanceOf(LazyCollection::class, $results);
776+
foreach ($results as $i => $result) {
777+
$this->assertEquals($data[$i]['name'], $result['name']);
778+
}
779+
}
762780
}

0 commit comments

Comments
 (0)