Reputation: 561
I'm trying to insert a list of documents in MongoDB, each document has the field "_id". So when I send a list like this
[{"_id":"2"}, {"_id":"1"}, {"_id":"3"}]
And at least one of the _id's is already in the DB, it shows an pymongo.errors.DuplicateKeyError
.
I need to use the insert_many
or any similar function and when I use it, it returns me a list with the not-inserted id's or the inserted ones.
I've seen that insert_many
inserts one by one, and when it finds the exception, it stops and doesn't insert the rest of the documents.
Do you know any alternative of txMongo that I can use that insert all the documents it can and return me those who couldn't be inserted?
I don't want to insert one by one 'cause I possibly send a huge list.
Upvotes: 0
Views: 118
Reputation: 39432
This might help you :
Performing Unordered Insert in MongoDB using PyMongo
Just set the unordered
attribute to false. Same can be seen here
Upvotes: 1