MongoDB\Driver\Manager
PHP Manual

MongoDB\Driver\Manager::executeDelete

(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeDeleteConvenience method for a single delete operation

Descrição

final public MongoDB\Driver\WriteResult MongoDB\Driver\Manager::executeDelete ( string $namespace , array|object $filter [, array $deleteOptions [, MongoDB\Driver\WriteConcern $writeConcern ]] )

Convenience method to execute a MongoDB\Driver\BulkWrite with only one delete operation.

Parâmetros

namespace

A fully qualified namespace (databaseName.collectionName)

filter

The search filter.

deleteOptions

deleteOptions
Option Type Description Default
limit boolean Delete all matching documents (limit=0), or only the first matching document (limit=1) 0

writeConcern

Optionally, a MongoDB\Driver\WriteConcern. If none given, default to the Write Concern set by the MongoDB Connection URI.

Valor Retornado

Returns MongoDB\Driver\WriteResult on success, throws exception (instanceof MongoDB\Driver\Exception) on failure.

Erros

Exemplos

Exemplo #1 MongoDB\Driver\Manager::executeDelete() example

<?php
$filter 
= array(
    
"title" => "mongodb",
);
$deleteOptions = array(
    
"limit" => 1,
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY100);

$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());
}
?>

O exemplo acima irá imprimir algo similar à:

Deleted 1 document(s)

Notas

Cuidado

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:

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).

Veja Também


MongoDB\Driver\Manager
PHP Manual