2727use MongoDB \Exception \UnsupportedException ;
2828use function is_array ;
2929use function is_object ;
30+ use function is_string ;
3031use 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 $ wireVersionForHintServerSideError = 5 ;
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,13 @@ public function execute(Server $server)
130145 throw UnsupportedException::collationNotSupported ();
131146 }
132147
148+ /* Server versions >= 3.4.0 raise errors for unknown update
149+ * options. For previous versions, the CRUD spec requires a client-side
150+ * error. */
151+ if (isset ($ this ->options ['hint ' ]) && ! server_supports_feature ($ server , self ::$ wireVersionForHintServerSideError )) {
152+ throw UnsupportedException::hintNotSupported ();
153+ }
154+
133155 $ inTransaction = isset ($ this ->options ['session ' ]) && $ this ->options ['session ' ]->isInTransaction ();
134156 if ($ inTransaction && isset ($ this ->options ['writeConcern ' ])) {
135157 throw UnsupportedException::writeConcernNotSupportedInTransaction ();
@@ -170,6 +192,10 @@ private function createDeleteOptions()
170192 $ deleteOptions ['collation ' ] = (object ) $ this ->options ['collation ' ];
171193 }
172194
195+ if (isset ($ this ->options ['hint ' ])) {
196+ $ deleteOptions ['hint ' ] = $ this ->options ['hint ' ];
197+ }
198+
173199 return $ deleteOptions ;
174200 }
175201
0 commit comments