Commit da84ba51 authored by Hannes Magnusson's avatar Hannes Magnusson

PHP-1309: Collection::replaceOne()

parent 13ded9a3
...@@ -81,6 +81,15 @@ try { ...@@ -81,6 +81,15 @@ try {
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
} }
$result = $collection->replaceOne(
array("nick" => "Bobby Fischer"),
array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway")
);
printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getNumModified());
$result = $collection->find();
foreach($result as $document) {
var_dump($document);
}
$result = $collection->deleteMany(array("citizen" => "Iceland")); $result = $collection->deleteMany(array("citizen" => "Iceland"));
printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved()); printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved());
......
...@@ -209,9 +209,18 @@ class Collection { ...@@ -209,9 +209,18 @@ class Collection {
function deleteMany(array $filter) { /* {{{ */ function deleteMany(array $filter) { /* {{{ */
return $this->_writeSingle($filter, self::DELETE, array("limit" => 0)); return $this->_writeSingle($filter, self::DELETE, array("limit" => 0));
} /* }}} */ } /* }}} */
function updateOne(array $filter, $update, array $options = array()) { /* {{{ */ function updateOne(array $filter, array $update, array $options = array()) { /* {{{ */
if (key($update)[0] != '$') {
throw new \RuntimeException("First key in \$update must be a \$operator");
}
return $this->_writeSingle($filter, self::UPDATE, $options, $update); return $this->_writeSingle($filter, self::UPDATE, $options, $update);
} /* }}} */ } /* }}} */
function replaceOne(array $filter, array $update, array $options = array()) { /* {{{ */
if (key($update)[0] == '$') {
throw new \RuntimeException("First key in \$update must NOT be a \$operator");
}
return $this->_writeSingle($filter, self::UPDATE, $options, array('$set' => $update));
} /* }}} */
function updateMany(array $filter, $update, array $options = array()) { /* {{{ */ function updateMany(array $filter, $update, array $options = array()) { /* {{{ */
return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update); return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update);
} /* }}} */ } /* }}} */
......
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