array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'mongodb-driver-manager.executebulkwritecommand.php', 1 => 'MongoDB\\Driver\\Manager::executeBulkWriteCommand', 2 => 'Execute write operations using the bulkWrite command', ), 'up' => array ( 0 => 'class.mongodb-driver-manager.php', 1 => 'MongoDB\\Driver\\Manager', ), 'prev' => array ( 0 => 'mongodb-driver-manager.executebulkwrite.php', 1 => 'MongoDB\\Driver\\Manager::executeBulkWrite', ), 'next' => array ( 0 => 'mongodb-driver-manager.executecommand.php', 1 => 'MongoDB\\Driver\\Manager::executeCommand', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/mongodb/mongodb/driver/manager/executebulkwritecommand.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>
(mongodb >=2.1.0)
MongoDB\Driver\Manager::executeBulkWriteCommand — Execute write operations using the bulkWrite command
$bulk, ?array $options = null): MongoDB\Driver\BulkWriteCommandResultExecutes one or more write operations on the primary server using the » bulkWrite command introduced in MongoDB 8.0.
A MongoDB\Driver\BulkWriteCommand can be constructed with one or more write operations of varying types (e.g. inserts, updates, and deletes). Each write operation may target a different collection.
The default value for the "writeConcern" option will be
inferred from an active transaction (indicated by the
"session" option), followed by the
connection URI.
bulk (MongoDB\Driver\BulkWriteCommand)options
| Option | Type | Description |
|---|---|---|
| session | MongoDB\Driver\Session |
Сеанс, по'язаний з операцією. |
| writeConcern | MongoDB\Driver\WriteConcern |
Вимога щодо запису. |
Returns MongoDB\Driver\BulkWriteCommandResult on success.
bulk does not contain any write operations.bulk has already been executed. MongoDB\Driver\BulkWriteCommand objects may not be executed multiple times."session", а вимога щодо запису не задоволена, викидається
MongoDB\Driver\Exception\InvalidArgumentException.Приклад #1 Mixed write operations
Mixed write operations (i.e. inserts, updates, and deletes) will be sent to the server using a single » bulkWrite command.
<?php
$manager = new MongoDB\Driver\Manager;
$bulk = new MongoDB\Driver\BulkWriteCommand;
// Delete documents from both collections
$bulk->deleteMany('db.coll_one', []);
$bulk->deleteMany('db.coll_two', []);
// Insert documents into two collections
$bulk->insertOne('db.coll_one', ['_id' => 1]);
$bulk->insertOne('db.coll_two', ['_id' => 2]);
$bulk->insertOne('db.coll_two', ['_id' => 3]);
// Update a document in "coll_one"
$bulk->updateOne('db.coll_one', ['_id' => 1], ['$set' => ['x' => 1]]);
$result = $manager->executeBulkWriteCommand($bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());
printf("Updated %d document(s)\n", $result->getModifiedCount());
?>Поданий вище приклад виведе:
Inserted 3 document(s) Updated 1 document(s)
Приклад #2 Ordered write operations causing an error
<?php
$manager = new MongoDB\Driver\Manager;
$bulk = new MongoDB\Driver\BulkWriteCommand;
$bulk->deleteMany('db.coll', []);
$bulk->insertOne('db.coll', ['_id' => 1]);
$bulk->insertOne('db.coll', ['_id' => 2]);
$bulk->insertOne('db.coll', ['_id' => 1]);
$bulk->insertOne('db.coll', ['_id' => 3]);
try {
$result = $manager->executeBulkWriteCommand($bulk);
} catch (MongoDB\Driver\Exception\BulkWriteCommandException $e) {
$result = $e->getPartialResult();
var_dump($e->getWriteErrors());
}
printf("Inserted %d document(s)\n", $result->getInsertedCount());
?>Поданий вище приклад виведе щось схоже на:
array(1) {
[3]=>
object(MongoDB\Driver\WriteError)#5 (4) {
["message"]=>
string(78) "E11000 duplicate key error collection: db.coll index: _id_ dup key: { _id: 1 }"
["code"]=>
int(11000)
["index"]=>
int(3)
["info"]=>
object(stdClass)#6 (0) {
}
}
}
Inserted 2 document(s)