(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
完全修飾形式の名前空間 (databaseName.collectionName)。
filter
検索フィルタ。
newObj
更新演算子の配列、あるいは置き換え用の完全なオブジェクト。
updateOptions
オプション | 型 | 説明 | デフォルト |
---|---|---|---|
multi | boolean | マッチする最初のドキュメントだけを更新する (multi=false)、あるいはマッチするすべてのドキュメントを更新する (multi=true) | false |
upsert | boolean | filter が既存のドキュメントにマッチしない場合に、newObj を新しいオブジェクトとして追加して、可能であれば任意のアトミック修飾子を filter に適用する。 |
0 |
writeConcern
オプションで、MongoDB\Driver\WriteConcern を指定します。省略した場合のデフォルトは、MongoDB Connection URI で設定したものとなります。
成功した場合に MongoDB\Driver\WriteResult を返します。失敗した場合に例外 (MongoDB\Driver\Exception のインスタンス) をスローします。
例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());
}
?>
上の例の出力は、 たとえば以下のようになります。
Updated 0 document(s) Matched 0 document(s) Upserted documents: 0
例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());
}
?>
上の例の出力は、 たとえば以下のようになります。
Updated 0 document(s) Matched 0 document(s) Upserted documents: 1 upsertedId[0]: object(BSON\ObjectID)#3 (1) { ["oid"]=> string(24) "54d2c0d14c245fbe70d32ccf" }
注意:
書き込みが失敗した場合は、 MongoDB\Driver\WriteResult::getWriteErrors() の配列内には MongoDB\Driver\WriteError がひとつだけしか含まれません。 また MongoDB\Driver\WriteError::getIndex() は常に 0 (バッチ内におけるこの操作のインデックス) となります。
注意:
MongoDB\Driver\WriteResult::getUpsertedIds() の配列内には BSON\ObjectID がひとつだけしか含まれません。 また、インデックスは常に 0 (バッチ内におけるこの操作のインデックス) となります。