(mongodb >= 1.0.0)
MongoDB\Driver\Exception\WriteException::getWriteResult — 返回失败写入操作的 WriteResult
返回失败写入操作的 MongoDB\Driver\WriteResult。可以使用 MongoDB\Driver\WriteResult::getWriteErrors() 和 MongoDB\Driver\WriteResult::getWriteConcernError() 方法获取有关故障的更多详细信息。
此函数没有参数。
失败写入操作的 MongoDB\Driver\WriteResult。
示例 #1 MongoDB\Driver\Exception\WriteException::getWriteResult() 示例
<?php
$manager = new MongoDB\Driver\Manager('mongodb://localhost');
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 1]);
try {
$manager->executeBulkWrite('db.collection', $bulk);
} catch (MongoDB\Driver\Exception\WriteException $e) {
$writeResult = $e->getWriteResult();
if ($writeConcernError = $writeResult->getWriteConcernError()) {
var_dump($writeConcernError);
}
if ($writeErrors = $writeResult->getWriteErrors()) {
var_dump($writeErrors);
}
}
?>
上面的示例将输出类似以下内容
array(1) { [0]=> object(MongoDB\Driver\WriteError)#5 (4) { ["message"]=> string(70) "E11000 duplicate key error index: db.collection.$_id_ dup key: { : 1 }" ["code"]=> int(11000) ["index"]=> int(1) ["info"]=> NULL } }