Commit d92248a6 authored by Hannes Magnusson's avatar Hannes Magnusson

PHP-1313: Implement Collection::findOneAndDelete()

parent 74067ee5
......@@ -136,6 +136,15 @@ try {
echo "Kasparov\n";
var_dump($result);
echo "Deleting him, he isn't Croatian just yet\n";
$result = $collection->findOneAndDelete(array("citizen" => "Croatia"));
var_dump($result);
echo "This should be empty\n";
$result = $collection->find(array());
foreach($result as $document) {
var_dump($document);
}
} catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit;
......
......@@ -368,6 +368,46 @@ class Collection {
} /* }}} */
/* {{{ findAndModify */
function findOneAndDelete(array $filter, array $options = array()) { /* {{{ */
$options = array_merge($this->getFindOneAndDeleteOptions(), $options);
$options = $this->_massageFindAndModifyOptions($options);
$cmd = array(
"findandmodify" => $this->collname,
"query" => $filter,
) + $options;
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
if ($doc["ok"]) {
return $doc["value"];
}
throw $this->_generateCommandException($doc);
} /* }}} */
function getFindOneAndDeleteOptions() { /* {{{ */
return array(
/**
* The maximum amount of time to allow the query to run.
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
"maxTimeMS" => 0,
/**
* Limits the fields to return for all matching documents.
*
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results
*/
"projection" => array(),
/**
* Determines which document the operation modifies if the query selects multiple documents.
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
"sort" => array(),
);
} /* }}} */
function findOneAndReplace(array $filter, array $replacement, array $options = array()) { /* {{{ */
if (key($replacement)[0] == '$') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment