(mongodb >=0.2.0)
MongoDB\Driver\Manager::executeUpdate — Convenience method for a single update operation
$namespace
, array|object $filter
, array|object $newObj
[, array $updateOptions
[, MongoDB\Driver\WriteConcern $writeConcern
]] )Convenience method to execute a MongoDB\Driver\BulkWrite with only one update operation.
namespace
A fully qualified namespace (databaseName.collectionName)
filter
The search filter.
newObj
Either an array of update operators, or the full object to be replaced with
updateOptions
Option | Type | Description | Default |
---|---|---|---|
multi | boolean | Update only the first matching document (multi=false), or all matching documents (multi=true) | false |
upsert | boolean | If filter does not match an existing document, insert the newObj as a new object, applying any atomic modifiers to the filter if applicable. |
0 |
writeConcern
Optionally, a MongoDB\Driver\WriteConcern. If none given, default to the Write Concern set by the MongoDB Connection URI.
Returns MongoDB\Driver\WriteResult on success, throws exception (instanceof MongoDB\Driver\Exception) on failure.
Example #1 MongoDB\Driver\Manager::executeUpdate() example
<?php
$criteria = array(
"tag" => "mongodb",
);
$document = array(
'$set' => array(
"rating" => 5,
),
);
$updateOptions = array(
"multi" => true,
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result = $manager->executeUpdate("mydb.collection", $criteria, $document, $updateOptions, $writeConcern);
printf("Updated %d document(s)\n", $result->getModifiedCount());
printf("Matched %d document(s)\n", $result->getMatchedCount());
printf("Upserted documents: %d\n", $result->getUpsertedCount());
foreach ($result->getUpsertedIds() as $index => $id) {
printf("upsertedId[%d]: ", $index);
var_dump($id);
}
/* If the WriteConcern could not be fulfilled */
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n", $error->getMessage(), $error->getCode(), var_export($error->getInfo(), true));
}
/* If the write could not happen at all */
foreach ($result->getWriteErrors() as $writeError) {
printf("%s (%d)\n", $error->getMessage(), $error->getCode());
}
?>
以上例程的输出类似于:
Updated 0 document(s) Matched 0 document(s) Upserted documents: 0
Example #2 MongoDB\Driver\Manager::executeUpdate() with upsert
<?php
$criteria = array(
"tag" => "mongodb",
);
$document = array(
'$set' => array(
"rating" => 5,
),
);
$updateOptions = array(
"multi" => false,
"upsert" => true,
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result = $manager->executeUpdate("mydb.collection", $criteria, $document, $updateOptions, $writeConcern);
printf("Updated %d document(s)\n", $result->getModifiedCount());
printf("Matched %d document(s)\n", $result->getMatchedCount());
printf("Upserted documents: %d\n", $result->getUpsertedCount());
foreach ($result->getUpsertedIds() as $index => $id) {
printf("upsertedId[%d]: ", $index);
var_dump($id);
}
/* If the WriteConcern could not be fulfilled */
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n", $writeConcernError->getMessage(), $writeConcernError->getCode(), var_export($writeConcernError->getInfo(), true));
}
/* If the write could not happen at all */
foreach ($result->getWriteErrors() as $writeError) {
printf("%s (%d)\n", $writeError->getMessage(), $writeError->getCode());
}
?>
以上例程的输出类似于:
Updated 0 document(s) Matched 0 document(s) Upserted documents: 1 upsertedId[0]: object(BSON\ObjectID)#3 (1) { ["oid"]=> string(24) "54d2c0d14c245fbe70d32ccf" }
Note:
On write failure, MongoDB\Driver\WriteResult::getWriteErrors() will only ever have one MongoDB\Driver\WriteError in the array, and MongoDB\Driver\WriteError::getIndex() will always be 0 (the index of this operation in the batch).
Note:
MongoDB\Driver\WriteResult::getUpsertedIds() will only ever have one BSON\ObjectID in the array, and the index always be 0 (the index of this operation in the batch).