MongoDB\Driver\Manager::executeBulkWrite
(mongodb >=1.0.0)
MongoDB\Driver\Manager::executeBulkWrite — 하나 이상의 쓰기 작업 실행
설명
final public MongoDB\Driver\Manager::executeBulkWrite(string $namespace
, MongoDB\Driver\BulkWrite $bulk
, array $options
= array()): MongoDB\Driver\WriteResult
주 서버에서 하나 이상의 쓰기 작업을 실행합니다.
MongoDB\Driver\BulkWrite는 다양한 유형(예: 업데이트, 삭제 및 삽입)의 하나 이상의 쓰기 작업으로 구성할 수 있습니다. 드라이버는 왕복을 최적화하기 위해 가능한 한 적은 수의 요청으로 동일한 유형의 작업을 서버에 보내려고 시도합니다.
매개변수
namespace
(string)- 정규화된 네임스페이스(예: "
databaseName.collectionName
"). bulk
(MongoDB\Driver\BulkWrite)- 실행할 쓰기입니다.
options
options
Option Type 설명 session MongoDB\Driver\Session 작업과 연결할 세션입니다. writeConcern MongoDB\Driver\WriteConcern 작업에 적용할 쓰기 관심사입니다.
반환 값
성공하면 MongoDB\Driver\WriteResult를 반환합니다.
오류/예외
-
bulk
에 쓰기 작업이 포함되어 있지 않으면 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. -
bulk
이 이미 실행된 경우 MongoDB\Driver\Exception\InvalidArgumentException을 던집니다. MongoDB\Driver\BulkWrite 개체는 여러 번 실행할 수 없습니다. -
"session"
옵션이 승인되지 않은 쓰기 문제와 함께 사용되는 경우 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. - 인수 구문 분석 오류에 대해 MongoDB\Driver\Exception\InvalidArgumentException을 던집니다.
- (인증 이외의 이유로) 서버 연결이 실패하면 MongoDB\Driver\Exception\ConnectionException을 던집니다.
- 인증이 필요하고 실패하면 MongoDB\Driver\Exception\AuthenticationException을 던집니다.
- 모든 쓰기 실패(예: 쓰기 오류, 쓰기 문제 적용 실패)에서 MongoDB\Driver\Exception\BulkWriteException을 던집니다.
- 다른 오류에 대해 MongoDB\Driver\Exception\RuntimeException을 던집니다.
변경 로그
버전 | 설명 |
---|---|
PECL mongodb 1.4.4 | "session" 옵션이 승인되지 않은 쓰기 문제와 함께 사용되면 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. |
PECL mongodb 1.4.0 | 세 번째 매개변수는 이제 options 배열입니다. 이전 버전과의 호환성을 위해 이 매개변수는 여전히 MongoDB\Driver\WriteConcern 개체를 허용합니다. |
PECL mongodb 1.3.0 | bulk 에 쓰기 작업이 포함되어 있지 않으면 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. 이전에는 MongoDB\Driver\Exception\BulkWriteException이 발생했습니다. |
Examples
예제 #1 MongoDB\Driver\Manager::executeBulkWrite() 예제
<?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)
기타
- MongoDB\Driver\BulkWrite
- MongoDB\Driver\WriteResult
- MongoDB\Driver\WriteConcern
- MongoDB\Driver\Server::executeBulkWrite() - 이 서버에서 하나 이상의 쓰기 작업을 실행합니다.