MongoDB\Driver\BulkWrite 클래스

(mongodb >=1.0.0)


소개

MongoDB\Driver\BulkWrite는 서버로 보내야 하는 하나 이상의 쓰기 작업을 수집합니다. 삽입, 업데이트 및 삭제 작업을 추가한 후 MongoDB\Driver\Manager::executeBulkWrite()를 통해 컬렉션을 실행할 수 있습니다.

쓰기 작업은 순서가 지정되거나(기본값) 순서가 지정되지 않을 수 있습니다. 순서가 지정된 쓰기 작업은 직렬 실행을 위해 제공된 순서대로 서버로 전송됩니다. 쓰기가 실패하면 나머지 작업이 중단됩니다. 순서가 지정되지 않은 작업은 병렬로 실행될 수 있는 임의의 순서로 서버로 전송됩니다. 발생하는 모든 오류는 모든 작업이 시도된 후에 보고됩니다.


클래스 개요

final class MongoDB\Driver\BulkWrite implements Countable {
  /* Methods */
  public __construct(array $options = ?)
  public count(): int
  public delete(array|object $filter, array $deleteOptions = ?): void
  public insert(array|object $document): mixed
  public update(array|object $filter, array|object $newObj, array $updateOptions = ?): void
}
                

Examples

예제 #1 혼합 쓰기 작업은 유형별로 그룹화됩니다.

혼합 쓰기 작업(예: 삽입, 업데이트 및 삭제)은 입력된 쓰기 명령으로 조합되어 서버에 순차적으로 전송됩니다.

                  
<?php

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->insert(['_id' => 1, 'x' => 1]);
$bulk->insert(['_id' => 2, 'x' => 2]);
$bulk->update(['x' => 2], ['$set' => ['x' => 1]]);
$bulk->insert(['_id' => 3, 'x' => 3]);
$bulk->delete(['x' => 1]);

?>
                  
                

4개의 쓰기 명령(예: 왕복)이 실행됩니다. 작업은 순서가 지정되어 있으므로 이전 업데이트가 실행될 때까지 세 번째 삽입을 보낼 수 없습니다.

예제 #2 오류를 유발하는 정렬된 쓰기 작업

                  
<?php

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 3, 'hello' => 'world']);
$bulk->update(['_id' => 3], ['$set' => ['hello' => 'earth']]);
$bulk->insert(['_id' => 4, 'hello' => 'pluto']);
$bulk->update(['_id' => 4], ['$set' => ['hello' => 'moon']]);
$bulk->insert(['_id' => 3]);
$bulk->insert(['_id' => 4]);
$bulk->insert(['_id' => 5]);

$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());

?>
                  
                

위의 예는 다음을 출력합니다.

Operation#7: E11000 duplicate key error index: db.collection.$_id_ dup key: { : 3 } (11000)
Inserted 4 document(s)
Updated  2 document(s)
                

쓰기 문제를 채울 수 없는 경우 위의 예는 다음과 같이 출력됩니다.

waiting for replication timed out (64): array (
  'wtimeout' => true,
)
Operation#7: E11000 duplicate key error index: databaseName.collectionName.$_id_ dup key: { : 3 } (11000)
Inserted 4 document(s)
Updated  2 document(s)
                

위의 예를 실행하지만 정렬되지 않은 쓰기를 허용하는 경우:

                  
<?php

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]);
/* ... */

?>
                  
                

위의 예는 다음을 출력합니다.

Operation#7: E11000 duplicate key error index: db.collection.$_id_ dup key: { : 3 } (11000)
Operation#8: E11000 duplicate key error index: db.collection.$_id_ dup key: { : 4 } (11000)
Inserted 5 document(s)
Updated  2 document(s)
                

기타


목차