(mongodb >=1.0.0)
MongoDB\Driver\Manager::executeBulkWrite — Execute one or more write operations
$namespace
, MongoDB\Driver\BulkWrite $bulk
[, MongoDB\Driver\WriteConcern $writeConcern
] )Executes one or more write operations on the primary server.
A MongoDB\Driver\BulkWrite can be constructed with one or more write operations of varying types (e.g. updates, deletes, and inserts). The driver will attempt to send operations of the same type to the server in as few requests as possible to optimize round trips.
namespace
(string)完全修飾形式の名前空間 ("databaseName.collectionName")。
bulk
(MongoDB\Driver\BulkWrite)実行する MongoDB\Driver\BulkWrite。
writeConcern
(MongoDB\Driver\WriteConcern)オプションの MongoDB\Driver\WriteConcern で、書き込み操作の適用先を指定します。省略した場合のデフォルトは、MongoDB Connection URI で設定したものとなります。
成功した場合に MongoDB\Driver\WriteResult を返します。
例1 MongoDB\Driver\Manager::executeBulkWrite() example
<?php
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(['_id' => 1, 'x' => 1]);
$bulk->insert(['_id' => 2, 'x' => 2]);
$bulk->update(['x' => 2], ['$set' => ['x' => 1]], ['multi' => false, 'upsert' => false]);
$bulk->update(['x' => 3], ['$set' => ['x' => 3]], ['multi' => false, 'upsert' => true]);
$bulk->update(['_id' => 3], ['$set' => ['x' => 3]], ['multi' => false, 'upsert' => true]);
$bulk->insert(['_id' => 4, 'x' => 2]);
$bulk->delete(['x' => 1], ['limit' => 1]);
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern);
printf("Inserted %d document(s)\n", $result->getInsertedCount());
printf("Matched %d document(s)\n", $result->getMatchedCount());
printf("Updated %d document(s)\n", $result->getModifiedCount());
printf("Upserted %d document(s)\n", $result->getUpsertedCount());
printf("Deleted %d document(s)\n", $result->getDeletedCount());
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 a write could not happen at all */
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n", $writeError->getIndex(), $writeError->getMessage(), $writeError->getCode());
}
?>
上の例の出力は、 たとえば以下のようになります。
Inserted 3 document(s) Matched 1 document(s) Updated 1 document(s) Upserted 2 document(s) Deleted 1 document(s) upsertedId[3]: object(MongoDB\BSON\ObjectID)#5 (1) { ["oid"]=> string(24) "54d3adc3ce7a792f4d703756" } upsertedId[4]: int(3)