MongoCursorException: E11000 Duplicate Key Error Index
Join the DZone community and get the full member experience.
Join For FreeI’m rocking along and all is working well for initial tests of the algorithm (header + first row of actual data) but when I turn-on processing for the other 9,999 rows, all I get stored into mongo is the first row of data.
I add an echo statement after the insert and I see 10,000 names scroll across my terminal. So the problem isn’t that I’m not getting the data, it’s that the data isn’t being stored into Mongo. I try turning on safe writes on my $mongo->insert() function and *bam*, error message:
PHP Fatal error: Uncaught exception ‘MongoCursorException’ with message ‘E11000 duplicate key error index: insite.testdata.$_id_ dup key: { : ObjectId(’4d25fd9a7e03972618000000′) }’ in /htdocs/framework/parseCSV.php:449 Stack trace: #0 /htdocs/framework/parseCSV.php(449): MongoCollection->insert(Object(mongorriffic), Array) #1 {main} thrown in /htdocs/framework/parseCSV.php on line 449
My code looks like this:
if (($handle = fopen($argv[1], "r")) !== FALSE) { while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { if (!$row) { // use the csv headers to instantiate the new mongo object $objMongo = new mongorriffic($data); } else { // parse the data into the existing structure $objMongo->storeData($data); $mongoCollection->insert($objMongo); } $row++; } fclose($handle); }
I’m calling my internal method (storeDate()) but I’m not creating, populating or resetting an “_id” field value because mongodb is supposed to automagically handle that for me. What’s actually happening is that mongodb is creating a valid “_id” value for the first record, but since my method does nothing to manipulate the field, the value persists through iterations of the data. I fix this by adding the following line of code after I invoke the insert method:
unset($objMongo->_id);
I re-run my script and *success*! 10,000 records are stored in less than 6-seconds using 4 collections. BTW, am *loving* Mongodb….
Published at DZone with permission of Micheal Shallop, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments