Skip to content

Commit cfeaec2

Browse files
committed
PHPLIB-266 Support passing index hint to aggregations
1 parent 929704d commit cfeaec2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

docs/includes/apiargs-MongoDBCollection-method-aggregate-option.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ post: |
3434
Document validation requires MongoDB 3.2 or later: if you are using an earlier
3535
version of MongoDB, this option will be ignored.
3636
---
37+
source:
38+
file: apiargs-MongoDBCollection-method-find-option.yaml
39+
ref: hint
40+
---
3741
source:
3842
file: apiargs-common-option.yaml
3943
ref: maxTimeMS

src/Operation/Aggregate.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class Aggregate implements Executable
7474
* This is not supported for server versions < 3.4 and will result in an
7575
* exception at execution time if used.
7676
*
77+
* * hint (string|document): The index to use. Specify either the index
78+
* name as a string or the index key pattern as a document. If specified,
79+
* then the query system will only consider plans using the hinted index.
80+
*
7781
* * maxTimeMS (integer): The maximum amount of time to allow the query to
7882
* run.
7983
*
@@ -146,6 +150,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
146150
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
147151
}
148152

153+
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
154+
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], 'string or array or object');
155+
}
156+
149157
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
150158
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
151159
}
@@ -272,6 +280,10 @@ private function createCommand(Server $server, $isCursorSupported)
272280
$cmd['collation'] = (object) $this->options['collation'];
273281
}
274282

283+
if (isset($this->options['hint'])) {
284+
$cmd['hint'] = $this->options['hint'];
285+
}
286+
275287
if (isset($this->options['maxTimeMS'])) {
276288
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
277289
}

tests/Operation/AggregateTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function provideInvalidConstructorOptions()
4444
$options[][] = ['collation' => $value];
4545
}
4646

47+
foreach ($this->getInvalidHintValues() as $value) {
48+
$options[][] = ['hint' => $value];
49+
}
50+
4751
foreach ($this->getInvalidIntegerValues() as $value) {
4852
$options[][] = ['maxTimeMS' => $value];
4953
}
@@ -98,4 +102,9 @@ public function testConstructorTypeMapOptionRequiresUseCursor()
98102
['typeMap' => ['root' => 'array'], 'useCursor' => false]
99103
);
100104
}
105+
106+
private function getInvalidHintValues()
107+
{
108+
return [123, 3.14, true];
109+
}
101110
}

0 commit comments

Comments
 (0)