Skip to content

Commit 324a20e

Browse files
committed
PHPLIB-491: Support hint for update command
Spec tests from mongodb/specifications@60adafc
1 parent ee83333 commit 324a20e

13 files changed

+877
-2
lines changed

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

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)