(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 espacio de nombres completamente cualificado (nombreBaseDatos.nombreColección)
filter
El filtro de búsquda.
deleteOptions
Opción | Tipo | Descripción | Predeterminado |
---|---|---|---|
limit | boolean | Eliminar todos los documentos coincidentes (limit=0), o solamente el primero que coincida (limit=1) | 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.
namespace
is not on the form 'databaseName.collectionName'.Ejemplo #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());
}
?>
El resultado del ejemplo sería algo similar a:
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.
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).