Skip to content

Commit fe6fcff

Browse files
committed
PHPLIB-539: Allow hinting for delete
1 parent 8b6f006 commit fe6fcff

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ source:
22
file: apiargs-MongoDBCollection-common-option.yaml
33
ref: collation
44
---
5+
source:
6+
file: apiargs-common-option.yaml
7+
ref: hint
8+
post: |
9+
This option is available in MongoDB 4.4+ and will result in an exception at
10+
execution time if specified for an older server version.
11+
12+
.. versionadded:: 1.7
13+
---
514
source:
615
file: apiargs-common-option.yaml
716
ref: session

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ source:
22
file: apiargs-MongoDBCollection-common-option.yaml
33
ref: collation
44
---
5+
source:
6+
file: apiargs-common-option.yaml
7+
ref: hint
8+
post: |
9+
This option is available in MongoDB 4.4+ and will result in an exception at
10+
execution time if specified for an older server version.
11+
12+
.. versionadded:: 1.7
13+
---
514
source:
615
file: apiargs-common-option.yaml
716
ref: session

src/Operation/Delete.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use MongoDB\Exception\UnsupportedException;
2828
use function is_array;
2929
use function is_object;
30+
use function is_string;
3031
use function MongoDB\server_supports_feature;
3132

3233
/**
@@ -43,6 +44,9 @@ class Delete implements Executable, Explainable
4344
/** @var integer */
4445
private static $wireVersionForCollation = 5;
4546

47+
/** @var int */
48+
private static $wireVersionForHint = 9;
49+
4650
/** @var string */
4751
private $databaseName;
4852

@@ -68,6 +72,13 @@ class Delete implements Executable, Explainable
6872
* This is not supported for server versions < 3.4 and will result in an
6973
* exception at execution time if used.
7074
*
75+
* * hint (string|document): The index to use. Specify either the index
76+
* name as a string or the index key pattern as a document. If specified,
77+
* then the query system will only consider plans using the hinted index.
78+
*
79+
* This is not supported for server versions < 4.4 and will result in an
80+
* exception at execution time if used.
81+
*
7182
* * session (MongoDB\Driver\Session): Client session.
7283
*
7384
* Sessions are not supported for server versions < 3.6.
@@ -97,6 +108,10 @@ public function __construct($databaseName, $collectionName, $filter, $limit, arr
97108
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
98109
}
99110

111+
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
112+
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], ['string', 'array', 'object']);
113+
}
114+
100115
if (isset($options['session']) && ! $options['session'] instanceof Session) {
101116
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
102117
}
@@ -130,6 +145,10 @@ public function execute(Server $server)
130145
throw UnsupportedException::collationNotSupported();
131146
}
132147

148+
if (isset($this->options['hint']) && ! server_supports_feature($server, self::$wireVersionForHint)) {
149+
throw UnsupportedException::hintNotSupported();
150+
}
151+
133152
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
134153
if ($inTransaction && isset($this->options['writeConcern'])) {
135154
throw UnsupportedException::writeConcernNotSupportedInTransaction();
@@ -170,6 +189,10 @@ private function createDeleteOptions()
170189
$deleteOptions['collation'] = (object) $this->options['collation'];
171190
}
172191

192+
if (isset($this->options['hint'])) {
193+
$deleteOptions['hint'] = $this->options['hint'];
194+
}
195+
173196
return $deleteOptions;
174197
}
175198

src/Operation/DeleteMany.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class DeleteMany implements Executable, Explainable
4545
* This is not supported for server versions < 3.4 and will result in an
4646
* exception at execution time if used.
4747
*
48+
* * hint (string|document): The index to use. Specify either the index
49+
* name as a string or the index key pattern as a document. If specified,
50+
* then the query system will only consider plans using the hinted index.
51+
*
52+
* This is not supported for server versions < 4.4 and will result in an
53+
* exception at execution time if used.
54+
*
4855
* * session (MongoDB\Driver\Session): Client session.
4956
*
5057
* Sessions are not supported for server versions < 3.6.

src/Operation/DeleteOne.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class DeleteOne implements Executable, Explainable
4545
* This is not supported for server versions < 3.4 and will result in an
4646
* exception at execution time if used.
4747
*
48+
* * hint (string|document): The index to use. Specify either the index
49+
* name as a string or the index key pattern as a document. If specified,
50+
* then the query system will only consider plans using the hinted index.
51+
*
52+
* This is not supported for server versions < 4.4 and will result in an
53+
* exception at execution time if used.
54+
*
4855
* * session (MongoDB\Driver\Session): Client session.
4956
*
5057
* Sessions are not supported for server versions < 3.6.

0 commit comments

Comments
 (0)