MongoDB\Driver\Manager::executeBulkWriteCommand
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
MongoDB\Driver\Manager::executeBulkWriteCommand
Референца за `mongodb-driver-manager.executebulkwritecommand.php` со подобрена типографија и навигација.
MongoDB\Driver\Manager::executeBulkWriteCommand
(mongodb >=2.1.0)
MongoDB\Driver\Manager::executeBulkWriteCommand — Execute write operations using the bulkWrite command
= NULL
$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.
А 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" опцијата ќе бидат изведени од активна трансакција (означена со
"session" опцијата), проследено со
URI за поврзување.
Параметри
bulk(MongoDB\Driver\BulkWriteCommand)-
The write(s) to execute.
options-
options Опција Тип = NULL session MongoDB\Driver\Session Сесија што ќе се поврзе со операцијата.
writeConcern MongoDB\Driver\WriteConcern Загриженост за пишување што ќе се примени на операцијата.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. MongoDB\Driver\BulkWriteCommandResult на успешен исход.
Errors/Exceptions
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException if
bulkdoes not contain any write operations. - ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException if
bulkhas already been executed. MongoDB\Driver\BulkWriteCommand objects may not be executed multiple times. - ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException ако
"session"опцијата се користи во комбинација со непризнаен запис на загриженост. - ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException » Промени текови
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\ConnectionException ако врската со серверот пропадне (од причини различни од автентикација).
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\AuthenticationException ако автентикацијата е потребна и пропадне.
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\BulkWriteCommandException on any write failure (e.g. command failure, write or write concern error)
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\RuntimeException on other errors.
Примери
Example #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());
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
Inserted 3 document(s) Updated 1 document(s)
Ќе резултира со извршување на четири команди за запишување (т.е. заобиколувања). Бидејќи операциите се нарачани, третото вметнување не може да се испрати додека не се изврши претходното ажурирање.
<?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)
Види Исто така
- MongoDB\Driver\BulkWriteCommand
- MongoDB\Driver\BulkWriteCommandResult
- MongoDB\Driver\WriteConcern
- MongoDB\Driver\Server::executeBulkWriteCommand() - Извршува операции за запишување на овој сервер користејќи ја командата bulkWrite