(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
Un espacio de nombres completamente cualificado (nombreBaseDatos.nombreColección)
filter
El filtro de búsquda.
newObj
Un array de operadores de actualización, o el objeto completo de remplazo
updateOptions
Opción | Tipo | Descripción | Predeterminado |
---|---|---|---|
multi | boolean | Actualizar solamente el primer documento que coincida (multi=false), o todos los documentos coincidentes (multi=true) | false |
upsert | boolean | Si filter no concuerda con un documento existente, se inserta newObj como un nuevo objeto, aplicando cualquier modificador atómico a filter si fuera aplicable. |
0 |
writeConcern
Opcionalmente, un MongoDB\Driver\WriteConcern. Si no se proporciona, se empleará el valor del Asunto de escritura establecido por el URI de conexión de MongoDB.
Returns MongoDB\Driver\WriteResult on success, lanza una excepción (instanceof MongoDB\Driver\Exception) en caso de fallo.
Ejemplo #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());
}
?>
El resultado del ejemplo sería algo similar a:
Updated 0 document(s) Matched 0 document(s) Upserted documents: 0
Ejemplo #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());
}
?>
El resultado del ejemplo sería algo similar a:
Updated 0 document(s) Matched 0 document(s) Upserted documents: 1 upsertedId[0]: object(BSON\ObjectID)#3 (1) { ["oid"]=> string(24) "54d2c0d14c245fbe70d32ccf" }
Nota:
En caso de fallo en una escritura MongoDB\Driver\WriteResult::getWriteErrors() solamente contendrá un MongoDB\Driver\WriteError en el array, y MongoDB\Driver\WriteError::getIndex() siempre será 0 (el índice de esta operación en el lote).
Nota:
MongoDB\Driver\WriteResult::getUpsertedIds() solamente contendrá un BSON\ObjectID en el array, y el índice siempre será 0 (el índice de esta operación en el lote).