(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 espace de noms totalement qualifié (databaseName.collectionName)
filter
Le filtre de recherche.
newObj
Soit un tableau d'opérateurs de mise à jour, soit l'objet complet qui servira pour les remplacements
updateOptions
Option | Type | Description | Défaut |
---|---|---|---|
multi | boolean | Met à jour que le premier document correspondant (multi=false), ou tous les documents correspondants (multi=true) | false |
upsert | boolean | Si filter ne permet pas
de trouver un document existant, on insère
le paramètre newObj comme nouvel objet,
appliquant les modificateurs atomiques au
filter si possible.
|
0 |
writeConcern
Optionnellement, une MongoDB\Driver\WriteConcern. Si rien n'est fourni, ce sera les préoccupations d'écriture définies par l' URI de connexion MongoDB.
Retourne le MongoDB\Driver\WriteResult en cas de succès, lance une exception (une instance de la classe MongoDB\Driver\Exception) en cas d'échec.
Exemple #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());
}
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
Updated 0 document(s) Matched 0 document(s) Upserted documents: 0
Exemple #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());
}
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
Updated 0 document(s) Matched 0 document(s) Upserted documents: 1 upsertedId[0]: object(BSON\ObjectID)#3 (1) { ["oid"]=> string(24) "54d2c0d14c245fbe70d32ccf" }
Note:
Lors d'une erreur pendant une écriture, MongoDB\Driver\WriteResult::getWriteErrors() va n'avoir qu'une MongoDB\Driver\WriteError dans son tableau, et MongoDB\Driver\WriteError::getIndex() vaudra toujours 0 (l'index de cette opération dans le lot).
Note:
MongoDB\Driver\WriteResult::getUpsertedIds() va n'avoir qu'une BSON\ObjectID dans son tableau, et l'index sera toujours de 0 (l'index de cette opération dans le lot).