@@ -472,6 +472,67 @@ function getFindOneAndReplaceOptions() { /* {{{ */
472
472
473
473
} /* }}} */
474
474
475
+ function findOneAndUpdate (array $ filter , array $ update , array $ options = array ()) { /* {{{ */
476
+ if (key ($ update )[0 ] != '$ ' ) {
477
+ throw new \RuntimeException ("First key in \$update must be a \$operator " );
478
+ }
479
+
480
+ $ options = array_merge ($ this ->getFindOneAndUpdateOptions (), $ options );
481
+ $ options = $ this ->_massageFindAndModifyOptions ($ options , $ update );
482
+
483
+ $ cmd = array (
484
+ "findandmodify " => $ this ->collname ,
485
+ "query " => $ filter ,
486
+ ) + $ options ;
487
+
488
+ $ doc = $ this ->_runCommand ($ this ->dbname , $ cmd )->getResponseDocument ();
489
+ if ($ doc ["ok " ]) {
490
+ return $ doc ["value " ];
491
+ }
492
+
493
+ throw $ this ->_generateCommandException ($ doc );
494
+ } /* }}} */
495
+ function getFindOneAndUpdateOptions () { /* {{{ */
496
+ return array (
497
+
498
+ /**
499
+ * The maximum amount of time to allow the query to run.
500
+ *
501
+ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
502
+ */
503
+ "maxTimeMS " => 0 ,
504
+
505
+ /**
506
+ * Limits the fields to return for all matching documents.
507
+ *
508
+ * @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results
509
+ */
510
+ "projection " => array (),
511
+
512
+ /**
513
+ * When ReturnDocument.After, returns the updated or inserted document rather than the original.
514
+ * Defaults to ReturnDocument.Before.
515
+ *
516
+ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
517
+ */
518
+ "returnDocument " => self ::FIND_ONE_AND_RETURN_BEFORE ,
519
+
520
+ /**
521
+ * Determines which document the operation modifies if the query selects multiple documents.
522
+ *
523
+ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
524
+ */
525
+ "sort " => array (),
526
+
527
+ /**
528
+ * When true, creates a new document if no document matches the query. The default is false.
529
+ *
530
+ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
531
+ */
532
+ "upsert " => false ,
533
+ );
534
+
535
+ } /* }}} */
475
536
476
537
protected function _massageFindAndModifyOptions ($ options , $ update = array ()) { /* {{{ */
477
538
$ ret = array (
0 commit comments