I get the following error when trying to do bulk insertion into an empty mongodb collection.
pymongo.errors.DuplicateKeyError: E11000 duplicate key error index: cmdDistros.locDistro. $ id dup key: {: ObjectId ('51dac9d0c74cd81acd85c0fd')}
I do not specify _id when creating any of the documents, so should mongodb create a unique index correctly? Here is the code I used:
#Populate database with uniform distribution entries = [] for coor in freeIndices: for theta in range(360): entry = {"x" : coor[0], "y" : coor[1], "heading" : theta} for i in range(numData): entry["data" + str(i)] = 1./numData entries.append(entry) print "Entries created, loading into database..." locDistro.insert(entries)
Taking fate from the hands of mongoDB, I tried to create my own index using:
#Populate database with uniform distribution entries = [] idNum = 0 for coor in freeIndices: for theta in range(360): print idNum entry = {"_id" : idNum, "x" : coor[0], "y" : coor[1], "heading" : theta} idNum += 1 for i in range(numData): entry["data" + str(i)] = 1./numData entries.append(entry) print "Entries created, loading into database..." locDistro.insert(entries, manipulate = False)
The print statement showed each idnum as documents were created and all of them were unique and increased in the same way as expected. However, on insertion I got an error:
pymongo.errors.DuplicateKeyError: E11000 duplicate key error index: cmdDistros.locDistro. $ id dup key: {: 0}
and only one document was inserted into my database.
I am completely at a standstill, everyone has an answer, why can this happen?