For bulk inserts where there may be duplicate key errors you need to set the continue_on_error
flag.
With this flag inserts will continue even if an error has happened. The last error will be reported back by getLastError
- which you can catch or if you want fire and forget set the write concern to 0.
from pymongo import *
client = MongoClient()
coll = client.test.test
coll.drop()
# Add a test document
coll.save({'_id': 1, 'hello': 'world'})
print(coll.count())
# Without the flag - Boom
coll.insert([{"_id": 1, "hello": "world"},
{"_id": 2, "Howdy": "Worldy"}])
print(coll.count())
# With a write concern of 0 - no error but not saved.
coll.insert([{"_id": 1, "hello": "world"},
{"_id": 2, "Howdy": "Worldy"}], w=0)
print(coll.count())
# Will error but will insert as well
coll.insert([{"_id": 1, "hello": "world"},
{"_id": 2, "Howdy": "Worldy"}], continue_on_error=True)
print(coll.count())
# With a continue_on_error and write concern of 0 - will only error if theres
# some socket or network error
coll.insert([{"_id": 1, "hello": "world"},
{"_id": 2, "Howdy": "Worldy"},
{"_id": 3, "Hi": "World"}], w=0, continue_on_error=True)
print(coll.count())