MongoDB\Driver\WriteResult::getWriteErrors
(mongodb >=1.0.0)
MongoDB\Driver\WriteResult::getWriteErrors — 발생한 쓰기 오류를 반환합니다.
설명
final public MongoDB\Driver\WriteResult::getWriteErrors(): array
매개변수
이 함수에는 매개변수가 없습니다.
반환 값
쓰기 작업 중 발생한 쓰기 오류에 대해 MongoDB\Driver\WriteError 개체의 배열을 반환합니다. 쓰기 오류가 발생하지 않으면 어레이가 비어 있습니다.
오류/예외
- 인수 구문 분석 오류에 대해 MongoDB\Driver\Exception\InvalidArgumentException을 던집니다.
Examples
예제 #1 단일 오류가 있는 MongoDB\Driver\WriteResult::getWriteErrors()
<?php
$manager = new MongoDB\Driver\Manager;
/* By default, bulk write operations are executed serially in order and
* execution will stop after the first error.
*/
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 3]);
$bulk->insert(['_id' => 4]);
$bulk->insert(['_id' => 4]);
try {
$result = $manager->executeBulkWrite('db.collection', $bulk);
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
var_dump($e->getWriteResult()->getWriteErrors());
}
?>
위의 예는 다음과 유사한 결과를 출력합니다.
array(1) { [0]=> object(MongoDB\Driver\WriteError)#5 (4) { ["message"]=> string(81) "E11000 duplicate key error collection: db.collection index: _id_ dup key: { : 2 }" ["code"]=> int(11000) ["index"]=> int(2) ["info"]=> NULL } }
예제 #2 여러 오류가 있는 MongoDB\Driver\WriteResult::getWriteErrors()
<?php
$manager = new MongoDB\Driver\Manager;
/* The "ordered" option may be used to allow bulk write operations to continue
* executing after the first error is encountered.
*/
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]);
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 3]);
$bulk->insert(['_id' => 4]);
$bulk->insert(['_id' => 4]);
try {
$result = $manager->executeBulkWrite('db.collection', $bulk);
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
var_dump($e->getWriteResult()->getWriteErrors());
}
?>
위의 예는 다음과 유사한 결과를 출력합니다.
array(2) { [0]=> object(MongoDB\Driver\WriteError)#5 (4) { ["message"]=> string(81) "E11000 duplicate key error collection: db.collection index: _id_ dup key: { : 2 }" ["code"]=> int(11000) ["index"]=> int(2) ["info"]=> NULL } [1]=> object(MongoDB\Driver\WriteError)#6 (4) { ["message"]=> string(81) "E11000 duplicate key error collection: db.collection index: _id_ dup key: { : 4 }" ["code"]=> int(11000) ["index"]=> int(5) ["info"]=> NULL } }