28
28
use function is_array ;
29
29
use function is_bool ;
30
30
use function is_object ;
31
+ use function is_string ;
31
32
use function MongoDB \is_first_key_operator ;
32
33
use function MongoDB \is_pipeline ;
33
34
use function MongoDB \server_supports_feature ;
@@ -52,6 +53,9 @@ class Update implements Executable, Explainable
52
53
/** @var integer */
53
54
private static $ wireVersionForDocumentLevelValidation = 4 ;
54
55
56
+ /** @var integer */
57
+ private static $ wireVersionForHint = 8 ;
58
+
55
59
/** @var string */
56
60
private $ databaseName ;
57
61
@@ -89,6 +93,13 @@ class Update implements Executable, Explainable
89
93
* This is not supported for server versions < 3.4 and will result in an
90
94
* exception at execution time if used.
91
95
*
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
+ *
92
103
* * multi (boolean): When true, updates all documents matching the query.
93
104
* This option cannot be true if the $update argument is a replacement
94
105
* document (i.e. contains no update operators). The default is false.
@@ -137,6 +148,10 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
137
148
throw InvalidArgumentException::invalidType ('"collation" option ' , $ options ['collation ' ], 'array or object ' );
138
149
}
139
150
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
+
140
155
if (! is_bool ($ options ['multi ' ])) {
141
156
throw InvalidArgumentException::invalidType ('"multi" option ' , $ options ['multi ' ], 'boolean ' );
142
157
}
@@ -187,6 +202,10 @@ public function execute(Server $server)
187
202
throw UnsupportedException::collationNotSupported ();
188
203
}
189
204
205
+ if (isset ($ this ->options ['hint ' ]) && ! server_supports_feature ($ server , self ::$ wireVersionForHint )) {
206
+ throw UnsupportedException::hintNotSupported ();
207
+ }
208
+
190
209
$ inTransaction = isset ($ this ->options ['session ' ]) && $ this ->options ['session ' ]->isInTransaction ();
191
210
if ($ inTransaction && isset ($ this ->options ['writeConcern ' ])) {
192
211
throw UnsupportedException::writeConcernNotSupportedInTransaction ();
@@ -261,8 +280,10 @@ private function createUpdateOptions()
261
280
'upsert ' => $ this ->options ['upsert ' ],
262
281
];
263
282
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
+ }
266
287
}
267
288
268
289
if (isset ($ this ->options ['collation ' ])) {
0 commit comments