Last active
April 3, 2017 17:03
-
-
Save christeredvartsen/d3571902e0b981e5950a5e8110bbcaa8 to your computer and use it in GitHub Desktop.
MongoDuplicateKeyException from MongoClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$client = new MongoClient(); | |
$client->selectDB('test')->drop(); | |
$collection = $client->selectCollection('test', 'image'); | |
$collection->createIndex( | |
['user' => 1, 'imageIdentifier' => 1], | |
['unique' => true] | |
); | |
$collection->insert([ | |
'user' => 'christer', | |
'imageIdentifier' => 'image1', | |
]); | |
try { | |
$collection->insert([ | |
'user' => 'christer', | |
'imageIdentifier' => 'image1', | |
]); | |
} catch (MongoDuplicateKeyException $e) { | |
var_dump($e->getMessage()); // string(167) "localhost:27017: insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.image.$user_1_imageIdentifier_1 dup key: { : "christer", : "image1" }" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment