@@ -91,6 +91,14 @@ class BulkWrite implements Executable
9191 * * upsert (boolean): When true, a new document is created if no document
9292 * matches the query. The default is false.
9393 *
94+ * Supported options for replaceOne and updateOne operations:
95+ *
96+ * * sort (document): Determines which document the operation modifies if
97+ * the query selects multiple documents.
98+ *
99+ * This is not supported for server versions < 8.0 and will result in an
100+ * exception at execution time if used.
101+ *
94102 * Supported options for updateMany and updateOne operations:
95103 *
96104 * * arrayFilters (document array): A set of filters specifying to which
@@ -372,6 +380,10 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
372380 throw InvalidArgumentException::expectedDocumentType (sprintf ('$operations[%d]["%s"][2]["collation"] ' , $ i , $ type ), $ args [2 ]['collation ' ]);
373381 }
374382
383+ if (isset ($ args [2 ]['sort ' ]) && ! is_document ($ args [2 ]['sort ' ])) {
384+ throw InvalidArgumentException::expectedDocumentType (sprintf ('$operations[%d]["%s"][2]["sort"] ' , $ i , $ type ), $ args [2 ]['sort ' ]);
385+ }
386+
375387 if (! is_bool ($ args [2 ]['upsert ' ])) {
376388 throw InvalidArgumentException::invalidType (sprintf ('$operations[%d]["%s"][2]["upsert"] ' , $ i , $ type ), $ args [2 ]['upsert ' ], 'boolean ' );
377389 }
@@ -413,6 +425,14 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
413425 throw InvalidArgumentException::expectedDocumentType (sprintf ('$operations[%d]["%s"][2]["collation"] ' , $ i , $ type ), $ args [2 ]['collation ' ]);
414426 }
415427
428+ if (isset ($ args [2 ]['sort ' ]) && ! is_document ($ args [2 ]['sort ' ])) {
429+ throw InvalidArgumentException::expectedDocumentType (sprintf ('$operations[%d]["%s"][2]["sort"] ' , $ i , $ type ), $ args [2 ]['sort ' ]);
430+ }
431+
432+ if (isset ($ args [2 ]['sort ' ]) && $ args [2 ]['multi ' ]) {
433+ throw new InvalidArgumentException (sprintf ('"sort" option cannot be used with $operations[%d]["%s"] ' , $ i , $ type ));
434+ }
435+
416436 if (! is_bool ($ args [2 ]['upsert ' ])) {
417437 throw InvalidArgumentException::invalidType (sprintf ('$operations[%d]["%s"][2]["upsert"] ' , $ i , $ type ), $ args [2 ]['upsert ' ], 'boolean ' );
418438 }
0 commit comments