Рекурсивный акроним словосочетания «PHP: Hypertext Preprocessor»
Добро пожаловать на форум PHP программистов!
За последние 24 часа нас посетили 17939 программистов и 1567 роботов. Сейчас ищут 1087 программистов ...
MongoDB\Driver\BulkWrite::__construct - Create a new BulkWrite
Вернуться к: MongoDB\Driver\BulkWrite
MongoDB\Driver\BulkWrite::__construct
(mongodb >=1.0.0)
MongoDB\Driver\BulkWrite::__construct — Create a new BulkWrite
Описание
public MongoDB\Driver\BulkWrite::__construct
([ array
$options
] )Constructs a new ordered (default) or unordered MongoDB\Driver\BulkWrite.
Список параметров
Ошибки
- Throws MongoDB\Driver\Exception\InvalidArgumentException on argument parsing errors.
Примеры
Пример #1 MongoDB\Driver\BulkWrite::__construct() example
<?php
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk->insert(['_id' => 1, 'x' => 1]);
$bulk->insert(['_id' => 2, 'x' => 2]);
$bulk->update(
['x' => 2],
['$set' => ['x' => 1]],
['limit' => 1, 'upsert' => false]
);
$bulk->delete(['x' => 1], ['limit' => 1]);
$bulk->update(
['_id' => 3],
['$set' => ['x' => 3]],
['limit' => 1, 'upsert' => true]
);
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
try {
$result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern);
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
$result = $e->getWriteResult();
// Check if the write concern could not be fulfilled
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n",
$writeConcernError->getMessage(),
$writeConcernError->getCode(),
var_export($writeConcernError->getInfo(), true)
);
}
// Check if any write operations did not complete at all
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n",
$writeError->getIndex(),
$writeError->getMessage(),
$writeError->getCode()
);
}
} catch (MongoDB\Driver\Exception\Exception $e) {
printf("Other error: %s\n", $e->getMessage());
exit;
}
printf("Inserted %d document(s)\n", $result->getInsertedCount());
printf("Updated %d document(s)\n", $result->getModifiedCount());
printf("Upserted %d document(s)\n", $result->getUpsertedCount());
printf("Deleted %d document(s)\n", $result->getDeletedCount());
?>
Результат выполнения данного примера:
Inserted 2 document(s) Updated 1 document(s) Upserted 1 document(s) Deleted 1 document(s)
Смотрите также
- MongoDB\Driver\Manager::executeBulkWrite() - Execute one or more write operations
- MongoDB\Driver\WriteResult
Вернуться к: MongoDB\Driver\BulkWrite