©
本文档使用 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.
Example #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 }
[#1] fastest963 at gmail dot com [2014-05-30 07:08:59]
Not sure about earlier versions but this definitely exists in pecl mongo 1.2.10.