(mongodb >=0.2.0)
MongoDB\Driver\Manager::executeDelete — Convenience method for a single delete operation
$namespace
, array|object $filter
[, array $deleteOptions
[, MongoDB\Driver\WriteConcern $writeConcern
]] )Convenience method to execute a MongoDB\Driver\BulkWrite with only one delete operation.
namespace
Un espace de noms totalement qualifié (databaseName.collectionName)
filter
Le filtre de recherche.
deleteOptions
Option | Type | Description | Défaut |
---|---|---|---|
limit | boolean | Supprime tous les documents correspondants (limit=0), ou seulement le premier document correspondant (limit=1) | 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.
namespace
is not on the form 'databaseName.collectionName'.Exemple #1 MongoDB\Driver\Manager::executeDelete() example
<?php
$filter = array(
"title" => "mongodb",
);
$deleteOptions = array(
"limit" => 1,
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result = $manager->executeDelete("mydb.collection", $filter, $deleteOptions, $writeConcern);
printf("Deleted %d document(s)\n", $result->getDeletedCount());
/* 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 à :
Deleted 1 document(s)
A single delete operation may delete more then one document.
The optional limit
deleteOption
should be treated as mandatory to avoid
accidents, or changes in the database defaults in the future.
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).