Duplicate Key Error on Upsert with multi processes(Mongo>=3.0.4 WiredTiger)
I found the bug at: https://jira.mongodb.org/browse/SERVER-14322
Please feel free to vote for it and watch it for further updates.
An upsert does both a check for an existing document to update, or inserts a new document.
My best guess is you are running into a timing issue where:
- Process 2 checks for existence, which it doesn't
- Process 1 checks for existence, which it doesn't
- Process 2 inserts, which works
- Process 1 inserts, which raises dupe key.
Check what native query your python library is sending underneath first. Confirm it's what you expect on the native mongo side. Then if you can reproduce this semi regularly on wiredtiger but never on mmap, raise a bug with mongo to confirm what their expected behavior is. It's sometimes hard to pick what they guarantee to be atomic.
This is a good example of why Mongo ObjectIDs combine a timestamp, a machine id, a pid and a counter for uniqueness.