MongoDB Insertion Enhances Key Repeatability

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?

+6
source share
2 answers

You need to understand that there is a bunch of links to the one entry in your list of entries. Therefore, when PyMongo sets the records [0] ['_ id'], all other records receive the same _id. (In fact, PyMongo will iterate over the list, specifying each _id entry, so all entries will have final _id at the end.) Quick fix:

 entries.append(entry.copy()) 

This is just a shallow copy, but in the code you shared, I believe that this is enough to fix your problem.

+11
source

Delete key _id

 for i in xrange(2): doc['i'] = i if '_id' in doc: **del doc['_id']** collection.insert(doc) 

* Or create manually ew onea n: Ask a question

 from bson.objectid import ObjectId* for i in xrange(2): doc['i'] = i doc['_id'] = ObjectId() collection.insert(doc) 

Getting "err": "E11000 duplicates a key error when pasting into mongo using Java driver

+2
source

Source: https://habr.com/ru/post/948948/


All Articles