mongodb multiple thread update one document ?

76 views
Skip to first unread message

Jie Yang

unread,
Mar 9, 2018, 7:20:11 AM3/9/18
to mongodb-user
Hi:
I meet a problem with mongodb .
multiple thread update one document with upsert options true.
contains $inc operation.

the code is like that blew.

val queryDoc = new Document("domainId."1").append("appId","1").append("date","2018-01-01");
val updateDoc = new Document().append("$inc", new Document("start", start).append("fresh", fresh).append("active", active))
 collection.updateOne( queryDoc, updateDoc,new UpdateOptions().upsert(true));

and I create index with domainId,appId,date field with uqiue index.

in multiple thread access to mongodb and do $inc operation ,some times occur

E11000 duplicate key err collection .

so how to aviod it?

Kevin Adistambha

unread,
Mar 19, 2018, 12:50:10 AM3/19/18
to mongodb-user

Hi

and I create index with domainId,appId,date field with uqiue index.

I believe the main issue that caused the duplicate key error was caused by the unique index and the code:

val queryDoc = new Document("domainId."1").append("appId","1").append("date","2018-01-01");

If you’re running the same code on two different threads, this queryDoc variable would contain the same value. Since you’re also defining a unique index on the collection, it’s possible that the two threads are finding no document matching the queryDoc and attempted to insert the document at the same time. Essentially this is a race condition in the application, and is described in SERVER-14322.

There are a couple of ways to avoid this error:

  • Do not use a unique key on the collection.
  • Have a logic in the application that retries the upsert when a duplicate key error was detected.

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages