php8.5
Home/ Manual/ driver / writeerror/ MongoDB\Driver\WriteError::getCode

MongoDB\Driver\WriteError::getCode

PHP function Edit on GitHub ✎

(mongodb >=1.0.0)

Returns the WriteError's error code

Description

MongoDB\Driver\WriteError::getCode(): int

Parameters Parameters

Return Values

Returns the WriteError's error code.

Errors/Exceptions

Examples

MongoDB\Driver\WriteError::getCode() example

php
<?php

$manager = new MongoDB\Driver\Manager;

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 1]);

try {
    $manager->executeBulkWrite('db.collection', $bulk);
} catch(MongoDB\Driver\Exception\BulkWriteException $e) {
    var_dump($e->getWriteResult()->getWriteErrors()[0]->getCode());
}

?>

The above example will output something similar to:

output
int(11000)

Source: reference/mongodb/mongodb/driver/writeerror/getcode.xml · from the official PHP manual (php/doc-en)