중복 키 오류가있는 곳의 경우 continue_on_error
flag을 설정해야합니다.
이 플래그를 사용하면 오류가 발생한 경우에도 삽입이 계속됩니다. 마지막 오류는 getLastError
에 의해 다시보고 될 것이다 - 당신이 잡을 또는 화재를 원하는 잊지 경우 write concern 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())
http://stackoverflow.com/questions/2801008/mongodb-insert-if을 설정할 수있는 -not-exists 그 Q를 보셨나요? – WiredPrairie