(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
完全修飾形式の名前空間 (databaseName.collectionName)。
filter
検索フィルタ。
deleteOptions
オプション | 型 | 説明 | デフォルト |
---|---|---|---|
limit | boolean | マッチするすべてのドキュメントを削除する (limit=0)、あるいはマッチした最初のドキュメントだけを削除する (limit=1) | 0 |
writeConcern
オプションで、MongoDB\Driver\WriteConcern を指定します。省略した場合のデフォルトは、MongoDB Connection URI で設定したものとなります。
成功した場合に MongoDB\Driver\WriteResult を返します。失敗した場合に例外 (MongoDB\Driver\Exception のインスタンス) をスローします。
namespace
is not on the form 'databaseName.collectionName'.例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());
}
?>
上の例の出力は、 たとえば以下のようになります。
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.
注意:
書き込みが失敗した場合は、 MongoDB\Driver\WriteResult::getWriteErrors() の配列内には MongoDB\Driver\WriteError がひとつだけしか含まれません。 また MongoDB\Driver\WriteError::getIndex() は常に 0 (バッチ内におけるこの操作のインデックス) となります。