Skip to content

Commit d92248a

Browse files
committed
PHP-1313: Implement Collection::findOneAndDelete()
1 parent 74067ee commit d92248a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

examples/write.php

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

139+
echo "Deleting him, he isn't Croatian just yet\n";
140+
$result = $collection->findOneAndDelete(array("citizen" => "Croatia"));
141+
var_dump($result);
142+
143+
echo "This should be empty\n";
144+
$result = $collection->find(array());
145+
foreach($result as $document) {
146+
var_dump($document);
147+
}
139148
} catch(Exception $e) {
140149
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
141150
exit;

src/MongoDB/Collection.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,46 @@ protected function _massageAggregateOptions($options) { /* {{{ */
368368
} /* }}} */
369369

370370
/* {{{ findAndModify */
371+
function findOneAndDelete(array $filter, array $options = array()) { /* {{{ */
372+
$options = array_merge($this->getFindOneAndDeleteOptions(), $options);
373+
$options = $this->_massageFindAndModifyOptions($options);
374+
$cmd = array(
375+
"findandmodify" => $this->collname,
376+
"query" => $filter,
377+
) + $options;
378+
379+
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
380+
if ($doc["ok"]) {
381+
return $doc["value"];
382+
}
383+
384+
throw $this->_generateCommandException($doc);
385+
} /* }}} */
386+
function getFindOneAndDeleteOptions() { /* {{{ */
387+
return array(
388+
389+
/**
390+
* The maximum amount of time to allow the query to run.
391+
*
392+
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
393+
*/
394+
"maxTimeMS" => 0,
395+
396+
/**
397+
* Limits the fields to return for all matching documents.
398+
*
399+
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results
400+
*/
401+
"projection" => array(),
402+
403+
/**
404+
* Determines which document the operation modifies if the query selects multiple documents.
405+
*
406+
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
407+
*/
408+
"sort" => array(),
409+
);
410+
} /* }}} */
371411

372412
function findOneAndReplace(array $filter, array $replacement, array $options = array()) { /* {{{ */
373413
if (key($replacement)[0] == '$') {

0 commit comments

Comments
 (0)