The MongoDuplicateKeyException class

(PECL mongo >= 1.5.0)

Вступ

Thrown when attempting to insert a document into a collection which already contains the same values for the unique keys.

Короткий Огляд Класа

MongoDuplicateKeyException extends MongoWriteConcernException {
/* Успадковані властивості */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Успадковані методи */
}

Приклади

Приклад #1 Catching MongoDuplicateKeyException

<?php
$mc 
= new MongoClient("localhost");

$c $mc->selectCollection("test""test");

$c->insert(array('_id' => 1));
try {
    
$c->insert(array('_id' => 1));
} catch (
MongoWriteConcernException $e) {
    echo 
$e->getMessage(), "\n";
}
?>

Наведені вище приклади виведуть щось подібне до:

localhost:27017: insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_  dup key: { : 1 }
add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top