Skip to content

Add cursor support #2024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Connection;
use MongoCollection;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;
use RuntimeException;

/**
* Class Builder
* @package Jenssegers\Mongodb\Query
*/
class Builder extends BaseBuilder
{
/**
Expand Down Expand Up @@ -209,12 +215,25 @@ public function get($columns = [])
return $this->getFresh($columns);
}

/**
* @inheritdoc
*/
public function cursor($columns = [])
{
$result = $this->getFresh($columns, true);
if ($result instanceof LazyCollection) {
return $result;
}
throw new RuntimeException("Query not compatible with cursor");
}

/**
* Execute the query as a fresh "select" statement.
* @param array $columns
* @return array|static[]|Collection
* @param bool $returnLazy
* @return array|static[]|Collection|LazyCollection
*/
public function getFresh($columns = [])
public function getFresh($columns = [], $returnLazy = false)
{
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
Expand Down Expand Up @@ -402,6 +421,14 @@ public function getFresh($columns = [])
// Execute query and get MongoCursor
$cursor = $this->collection->find($wheres, $options);

if ($returnLazy) {
return LazyCollection::make(function () use ($cursor) {
foreach ($cursor as $item) {
yield $item;
}
});
}

// Return results as an array with numeric keys
$results = iterator_to_array($cursor, false);
return $this->useCollections ? new Collection($results) : $results;
Expand Down
18 changes: 18 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use Jenssegers\Mongodb\Collection;
use Jenssegers\Mongodb\Query\Builder;
use MongoDB\BSON\ObjectId;
Expand Down Expand Up @@ -759,4 +760,21 @@ public function testHintOptions()
$this->assertEquals('spork', $results[1]['name']);
$this->assertEquals('fork', $results[0]['name']);
}

public function testCursor()
{
$data = [
['name' => 'fork', 'tags' => ['sharp', 'pointy']],
['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']],
['name' => 'spoon', 'tags' => ['round', 'bowl']],
];
DB::collection('items')->insert($data);

$results = DB::collection('items')->orderBy('_id', 'asc')->cursor();

$this->assertInstanceOf(LazyCollection::class, $results);
foreach ($results as $i => $result) {
$this->assertEquals($data[$i]['name'], $result['name']);
}
}
}