Skip to content

Commit 124f8ad

Browse files
committed
PHP-1315: Implement Collection::findOneAndUpdate()
1 parent d92248a commit 124f8ad

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

examples/write.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
echo "Kasparov\n";
137137
var_dump($result);
138138

139+
echo "Returning the old document where he was Russian\n";
140+
$result = $collection->findOneAndUpdate($kasparov, array('$set' => array("citizen" => "Croatia")));
141+
var_dump($result);
142+
139143
echo "Deleting him, he isn't Croatian just yet\n";
140144
$result = $collection->findOneAndDelete(array("citizen" => "Croatia"));
141145
var_dump($result);

src/MongoDB/Collection.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,67 @@ function getFindOneAndReplaceOptions() { /* {{{ */
472472

473473
} /* }}} */
474474

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+
} /* }}} */
475536

476537
protected function _massageFindAndModifyOptions($options, $update = array()) { /* {{{ */
477538
$ret = array(

0 commit comments

Comments
 (0)