Skip to content

Commit 2c95ab7

Browse files
divinemanan-jadhav
andcommitted
allow setting hint option on querybuilder
Co-Authored-By: Manan Jadhav <jadhavm@uwindsor.ca>
1 parent c25900b commit 2c95ab7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ public function getFresh($columns = [])
384384
if ($this->limit) {
385385
$options['limit'] = $this->limit;
386386
}
387+
if ($this->hint) {
388+
$options['hint'] = $this->hint;
389+
}
387390
if ($columns) {
388391
$options['projection'] = $columns;
389392
}
390-
// if ($this->hint) $cursor->hint($this->hint);
391393

392394
// Fix for legacy support, converts the results to arrays instead of objects.
393395
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];

tests/QueryBuilderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,25 @@ public function testValue()
737737
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
738738
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
739739
}
740+
741+
public function testHintOptions()
742+
{
743+
DB::collection('items')->insert([
744+
['name' => 'fork', 'tags' => ['sharp', 'pointy']],
745+
['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']],
746+
['name' => 'spoon', 'tags' => ['round', 'bowl']],
747+
]);
748+
749+
$results = DB::collection('items')->hint(['$natural' => -1])->get();
750+
751+
$this->assertArraySubset(['name' => 'spoon'], $results[0]);
752+
$this->assertArraySubset(['name' => 'spork'], $results[1]);
753+
$this->assertArraySubset(['name' => 'fork'], $results[2]);
754+
755+
$results = DB::collection('items')->hint(['$natural' => 1])->get();
756+
757+
$this->assertArraySubset(['name' => 'spoon'], $results[2]);
758+
$this->assertArraySubset(['name' => 'spork'], $results[1]);
759+
$this->assertArraySubset(['name' => 'fork'], $results[0]);
760+
}
740761
}

0 commit comments

Comments
 (0)