略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoDB\Driver\WriteResult::getWriteErrors

2024-03-29

MongoDB\Driver\WriteResult::getWriteErrors

(mongodb >=1.0.0)

MongoDB\Driver\WriteResult::getWriteErrorsReturns any write errors that occurred

说明

final public MongoDB\Driver\WriteResult::getWriteErrors(): array

参数

此函数没有参数。

返回值

Returns an array of MongoDB\Driver\WriteError objects for any write errors encountered during the write operation. The array will be empty if no write errors occurred.

错误/异常

范例

示例 #1 MongoDB\Driver\WriteResult::getWriteErrors() with a single error

<?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() with multiple errors

<?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
  }
}
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/mongodb-driver-writeresult.getwriteerrors.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3