Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "err" : "E11000 duplicate key error when inserting into mongo using the Java driver

Exception in thread "main" com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "E11000 duplicate key error index: twitterdb03.LevelAFollowers.$id dup key: { : ObjectId('52d5636de408652b4853a8fe') }" , "code" : 11000 , "n" : 0 , "connectionId" : 12 , "ok" : 1.0}

I'm using mongo 2.11.1

Never had problems with simple write operations in java

myMap.put(inid, followersList);
myObj.putAll(myMap);
myIdMapCollection.insert(myObj);
like image 347
Nautilus_o Avatar asked Jan 14 '14 17:01

Nautilus_o


People also ask

How do I avoid duplicate errors in Mongodb?

If you ever faced this error all you need to do is to check your model carefully and find out that is there any unique key set true by you and if it is not necessary then simply remove the unique key from the model or otherwise set a unique value if it is necessary to be unique.

How do I fix duplicate key errors?

There are the following options for handling duplicate key errors: You can delete the file, for example if you know that there are values in the file that are not correct. You can then adjust the file and upload it again. The system will not transfer any data from files that contain duplicate instances.


2 Answers

I found an answer on this page. I’m guessing your code looks something like this (greatly simplified)?:

doc = {} 
for i in xrange(2): 
    doc['i'] = i 
    collection.insert(doc) 

The problem is that PyMongo injects an _id field into the document, if the _id field does not exist, before inserting it (_id is always generated client side with 10gen drivers). That means that the first time through the loop _id is added by the insert method. Since doc is defined outside the loop, each subsequent pass through the loop uses the same value for _id.

Solution:

  1. Delete the key _id
for i in xrange(2): 
    doc['i'] = i 
    if '_id' in doc: 
        del doc['_id'] 
    collection.insert(doc)
  1. Or create manually a new one:
from bson.objectid import ObjectId 
for i in xrange(2): 
    doc['i'] = i 
    doc['_id'] = ObjectId() 
    collection.insert(doc)
like image 88
Florian Avatar answered Oct 05 '22 04:10

Florian


Try calling myIdMapCollection.save(myObj); instead of myIdMapCollection.insert(myObj);

The save method, unlike insert does upsert, meaning if a document contains _id, it replaces that document.

My guess is that you had fetched the DBObject using a cursor | query, had manipulated it, and you want to persist the changes. In that case, save is the right way to do it.

So, when calling insert the DBObject is already associated with _id, calling insert thus fails, because you already have a document with that _id in the collection, which should be unique (duplicate index error).

like image 45
Ori Dar Avatar answered Oct 05 '22 04:10

Ori Dar