Php/docs/class.mongoduplicatekeyexception

提供:Dev Guides
< Php
移動先:案内検索

(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 }