DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. MongoCursorException: E11000 Duplicate Key Error Index

MongoCursorException: E11000 Duplicate Key Error Index

Micheal Shallop user avatar by
Micheal Shallop
·
Jun. 23, 12 · Interview
Like (0)
Save
Tweet
Share
11.98K Views

Join the DZone community and get the full member experience.

Join For Free
So I’m working on a project where I’m taking a csv file that contains a little more than 100 columns of data by 10,000 rows.  (It’s a sample db file — the final file will be about 200,000,000 rows…) and writing a PHP script to process the csv file into structures that can be inserted as mongodb collections.

I’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….

Data (computing) Row (database) MongoDB CSV Record (computer science) Testing Column (database) Echo (command)

Published at DZone with permission of Micheal Shallop, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Right Framework for Your Project
  • Configure Kubernetes Health Checks
  • Demystifying the Infrastructure as Code Landscape
  • Java Concurrency: LockSupport

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: