Skip to content

Commit f4406d7

Browse files
committed
Merge pull request #717
2 parents 1ba76bf + 324a20e commit f4406d7

19 files changed

+897
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ post: |
2323
.. versionadded:: 1.4
2424
---
2525
source:
26-
file: apiargs-aggregate-option.yaml
26+
file: apiargs-common-option.yaml
2727
ref: hint
2828
post: |
2929
.. versionadded:: 1.3

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,11 @@ interface: phpmethod
8787
operation: ~
8888
optional: true
8989
---
90-
arg_name: option
91-
name: hint
92-
type: string|array|object
93-
description: |
94-
The index to use. Specify either the index name as a string or the index key
95-
pattern as a document. If specified, then the query system will only consider
96-
plans using the hinted index.
97-
90+
source:
91+
file: apiargs-common-option.yaml
92+
ref: hint
93+
post: |
9894
.. versionadded:: 1.2
99-
interface: phpmethod
100-
operation: ~
101-
optional: true
10295
---
10396
arg_name: option
10497
name: maxAwaitTimeMS

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ source:
1919
ref: comment
2020
---
2121
source:
22-
file: apiargs-MongoDBCollection-method-find-option.yaml
22+
file: apiargs-common-option.yaml
2323
ref: hint
24+
post: |
25+
.. versionadded:: 1.2
2426
---
2527
source:
2628
file: apiargs-common-option.yaml

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ source:
1010
file: apiargs-MongoDBCollection-common-option.yaml
1111
ref: collation
1212
---
13+
source:
14+
file: apiargs-common-option.yaml
15+
ref: hint
16+
post: |
17+
This option is available in MongoDB 4.2+ and will result in an exception at
18+
execution time if specified for an older server version.
19+
20+
.. versionadded:: 1.6
21+
---
1322
source:
1423
file: apiargs-common-option.yaml
1524
ref: session

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ source:
1616
file: apiargs-MongoDBCollection-common-option.yaml
1717
ref: collation
1818
---
19+
source:
20+
file: apiargs-common-option.yaml
21+
ref: hint
22+
post: |
23+
This option is available in MongoDB 4.2+ and will result in an exception at
24+
execution time if specified for an older server version.
25+
26+
.. versionadded:: 1.6
27+
---
1928
source:
2029
file: apiargs-common-option.yaml
2130
ref: session

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ source:
1616
file: apiargs-MongoDBCollection-common-option.yaml
1717
ref: collation
1818
---
19+
source:
20+
file: apiargs-common-option.yaml
21+
ref: hint
22+
post: |
23+
This option is available in MongoDB 4.2+ and will result in an exception at
24+
execution time if specified for an older server version.
25+
26+
.. versionadded:: 1.6
27+
---
1928
source:
2029
file: apiargs-common-option.yaml
2130
ref: session

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ source:
1919
ref: explain
2020
---
2121
source:
22-
file: apiargs-aggregate-option.yaml
22+
file: apiargs-common-option.yaml
2323
ref: hint
2424
---
2525
source:

docs/includes/apiargs-aggregate-option.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,4 @@ description: |
4949
interface: phpmethod
5050
operation: ~
5151
optional: true
52-
---
53-
arg_name: option
54-
name: hint
55-
type: string|array|object
56-
description: |
57-
The index to use. Specify either the index name as a string or the index key
58-
pattern as a document. If specified, then the query system will only consider
59-
plans using the hinted index.
60-
interface: phpmethod
61-
operation: ~
62-
optional: true
6352
...

docs/includes/apiargs-common-option.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ operation: ~
1616
optional: true
1717
---
1818
arg_name: option
19+
name: hint
20+
type: string|array|object
21+
description: |
22+
The index to use. Specify either the index name as a string or the index key
23+
pattern as a document. If specified, then the query system will only consider
24+
plans using the hinted index.
25+
interface: phpmethod
26+
operation: ~
27+
optional: true
28+
---
29+
arg_name: option
1930
name: maxTimeMS
2031
type: integer
2132
description: |

src/Exception/UnsupportedException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public static function explainNotSupported()
4949
return new static('Explain is not supported by the server executing this operation');
5050
}
5151

52+
/**
53+
* Thrown when a command's hint option is not supported by a server.
54+
*
55+
* @return self
56+
*/
57+
public static function hintNotSupported()
58+
{
59+
return new static('Hint is not supported by the server executing this operation');
60+
}
61+
5262
/**
5363
* Thrown when a command's readConcern option is not supported by a server.
5464
*

src/Operation/ReplaceOne.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ class ReplaceOne implements Executable
5555
* This is not supported for server versions < 3.4 and will result in an
5656
* exception at execution time if used.
5757
*
58+
* * hint (string|document): The index to use. Specify either the index
59+
* name as a string or the index key pattern as a document. If specified,
60+
* then the query system will only consider plans using the hinted index.
61+
*
62+
* This is not supported for server versions < 4.2 and will result in an
63+
* exception at execution time if used.
64+
*
5865
* * session (MongoDB\Driver\Session): Client session.
5966
*
6067
* Sessions are not supported for server versions < 3.6.

src/Operation/Update.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use function is_array;
2929
use function is_bool;
3030
use function is_object;
31+
use function is_string;
3132
use function MongoDB\is_first_key_operator;
3233
use function MongoDB\is_pipeline;
3334
use function MongoDB\server_supports_feature;
@@ -52,6 +53,9 @@ class Update implements Executable, Explainable
5253
/** @var integer */
5354
private static $wireVersionForDocumentLevelValidation = 4;
5455

56+
/** @var integer */
57+
private static $wireVersionForHint = 8;
58+
5559
/** @var string */
5660
private $databaseName;
5761

@@ -89,6 +93,13 @@ class Update implements Executable, Explainable
8993
* This is not supported for server versions < 3.4 and will result in an
9094
* exception at execution time if used.
9195
*
96+
* * hint (string|document): The index to use. Specify either the index
97+
* name as a string or the index key pattern as a document. If specified,
98+
* then the query system will only consider plans using the hinted index.
99+
*
100+
* This is not supported for server versions < 4.2 and will result in an
101+
* exception at execution time if used.
102+
*
92103
* * multi (boolean): When true, updates all documents matching the query.
93104
* This option cannot be true if the $update argument is a replacement
94105
* document (i.e. contains no update operators). The default is false.
@@ -137,6 +148,10 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
137148
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
138149
}
139150

151+
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
152+
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], ['string', 'array', 'object']);
153+
}
154+
140155
if (! is_bool($options['multi'])) {
141156
throw InvalidArgumentException::invalidType('"multi" option', $options['multi'], 'boolean');
142157
}
@@ -187,6 +202,10 @@ public function execute(Server $server)
187202
throw UnsupportedException::collationNotSupported();
188203
}
189204

205+
if (isset($this->options['hint']) && ! server_supports_feature($server, self::$wireVersionForHint)) {
206+
throw UnsupportedException::hintNotSupported();
207+
}
208+
190209
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
191210
if ($inTransaction && isset($this->options['writeConcern'])) {
192211
throw UnsupportedException::writeConcernNotSupportedInTransaction();
@@ -261,8 +280,10 @@ private function createUpdateOptions()
261280
'upsert' => $this->options['upsert'],
262281
];
263282

264-
if (isset($this->options['arrayFilters'])) {
265-
$updateOptions['arrayFilters'] = $this->options['arrayFilters'];
283+
foreach (['arrayFilters', 'hint'] as $option) {
284+
if (isset($this->options[$option])) {
285+
$updateOptions[$option] = $this->options[$option];
286+
}
266287
}
267288

268289
if (isset($this->options['collation'])) {

src/Operation/UpdateMany.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class UpdateMany implements Executable, Explainable
6161
* This is not supported for server versions < 3.4 and will result in an
6262
* exception at execution time if used.
6363
*
64+
* * hint (string|document): The index to use. Specify either the index
65+
* name as a string or the index key pattern as a document. If specified,
66+
* then the query system will only consider plans using the hinted index.
67+
*
68+
* This is not supported for server versions < 4.2 and will result in an
69+
* exception at execution time if used.
70+
*
6471
* * session (MongoDB\Driver\Session): Client session.
6572
*
6673
* Sessions are not supported for server versions < 3.6.

src/Operation/UpdateOne.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class UpdateOne implements Executable, Explainable
6161
* This is not supported for server versions < 3.4 and will result in an
6262
* exception at execution time if used.
6363
*
64+
* * hint (string|document): The index to use. Specify either the index
65+
* name as a string or the index key pattern as a document. If specified,
66+
* then the query system will only consider plans using the hinted index.
67+
*
68+
* This is not supported for server versions < 4.2 and will result in an
69+
* exception at execution time if used.
70+
*
6471
* * session (MongoDB\Driver\Session): Client session.
6572
*
6673
* Sessions are not supported for server versions < 3.6.

0 commit comments

Comments
 (0)