When performing load tests, I sometimes encounter a situation when this code (simplified):
var person = Persons.findOne();
if(person == null){
Persons.insert(newDocument);
}
throws an error, because the insert operation conflicts with an existing document - even though I just checked that using findOne(). Mongo shell shows the document when pausing before the insert().
The error is:
WriteConcern detected an error ''. (Response was { "ok" : 1, "code" : 11000, "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: MainDB.Persons.$Person UniquePersonID dup key: { : \"BOT 878611 141152\" }", "n" : NumberLong(0) }).
The unique index is:
{ v: 1, name: "Person UniquePersonID", key: { UniquePersonID : 1 }, unique: true, ns: "MainDB.Persons", sparse: true, dropDups: true, background: true }
This smells like a race condition, and I'm trying to find the right way to resolve this:
The problem with those methods is that 1+2 only replace certain fields and I want to replace the entire document, and method 3 replaces an entire document but the _id is conflicted.
(If relevant, I'm using the official C# driver version 1.9.2.235)
Appreciate your help.
OK, if I am understanding correctly, you want to do the following:
Your code hits the duplicate key error for the reason you think it does - another thread inserts the same UniquePersonId between the findOne
and the insert
. That's fine - the error just tells the code that some other process has done its work already. The unique index violation error isn't an error in the bad sense that something's not working right. It's just letting you know your operation is invalid because of the unique constraint that you imposed. I think (without knowing more about the context of this) that you can run the insert and just swallow the unique index error if it occurs.