diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 28d781a66..558edbdf3 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -384,10 +384,12 @@ public function getFresh($columns = []) if ($this->limit) { $options['limit'] = $this->limit; } + if ($this->hint) { + $options['hint'] = $this->hint; + } if ($columns) { $options['projection'] = $columns; } - // if ($this->hint) $cursor->hint($this->hint); // Fix for legacy support, converts the results to arrays instead of objects. $options['typeMap'] = ['root' => 'array', 'document' => 'array']; diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index bc30ad66b..625e59cad 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -737,4 +737,25 @@ public function testValue() $this->assertEquals('Herman', DB::collection('books')->value('author.first_name')); $this->assertEquals('Melville', DB::collection('books')->value('author.last_name')); } + + public function testHintOptions() + { + DB::collection('items')->insert([ + ['name' => 'fork', 'tags' => ['sharp', 'pointy']], + ['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']], + ['name' => 'spoon', 'tags' => ['round', 'bowl']], + ]); + + $results = DB::collection('items')->hint(['$natural' => -1])->get(); + + $this->assertEquals('spoon', $results[0]['name']); + $this->assertEquals('spork', $results[1]['name']); + $this->assertEquals('fork', $results[2]['name']); + + $results = DB::collection('items')->hint(['$natural' => 1])->get(); + + $this->assertEquals('spoon', $results[2]['name']); + $this->assertEquals('spork', $results[1]['name']); + $this->assertEquals('fork', $results[0]['name']); + } }