[SERVER-59758] findAndModify with upsert=true generates duplicate key errors Created: 02/Sep/21  Updated: 06/Dec/22  Resolved: 29/Sep/21

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.2.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Gaël Jourdan-Weil Assignee: Backlog - Triage Team
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Cloners
is cloned by SERVER-47212 Retry full upsert path when duplicate... Closed
Duplicate
duplicates SERVER-47212 Retry full upsert path when duplicate... Closed
Related
related to SERVER-14322 Retry on predicate unique index viola... Closed
Assigned Teams:
Server Triage
Operating System: ALL
Participants:

 Description   

This is very similar to SERVER-14322 however I'm in a case where the auto retry should happen from what I understand but it does not.

 

Error:

E11000 duplicate key error collection: dbName.collectionName index: a_1_b_1 dup key: { a: 31910, b: "2021-09-02T00:00:00Z" }

Indexes

 

> db.collectionName.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "databaseName.collectionName"
	},
	{
		"v" : 2,
		"unique" : true,
		"key" : {
			"a" : 1,
			"b" : 1
		},
		"name" : "a_1_b_1",
		"ns" : "databaseName.collectionName",
		"sparse" : true
	}
]

 

 

Commands run (though ReactiveMongo Scala):

> db.collectionName.findAndModify(
  query: { a: 31910, b: "2021-09-02T00:00:00Z" },
  update: { $inc: { c: 1 } },
  new: true
  upsert: true
)

 

In our use case, 8 commands are sent at the same time and sometimes we get 1 such error (not always).

 

Current document matching:

> db.collectionName.find({a:31910, b: "2021-09-02T00:00:00Z"})
{ "_id" : ObjectId("613062a3a00a6ac1f7687e5e"), "b" : "2021-09-02T00:00:00Z", "a" : 31910, "c" : 389723 }

Is this expected? Am I missing something?

We can add some retry mechanism in our code but I'd like to understand why the auto retry does not work.

 

Any comment will be appreciated.



 Comments   
Comment by Edwin Zhou [ 29/Sep/21 ]

Hi gjourdanweil@gmail.com,

Thanks for following up! Given that we've discussed that this a risky backport, I will close this as a duplicate of SERVER-47212 based on the discussion you had with james.wahlin on SERVER-14322.

Best,
Edwin

Comment by Gaël Jourdan-Weil [ 28/Sep/21 ]

From https://jira.mongodb.org/browse/SERVER-14322?focusedCommentId=4087196&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-4087196:

SERVER-47212 tracks this issue for the findAndModify command. The fix is currently available in MongoDB 5.0, but was deemed as risky to backport to 4.4 or earlier.

Generated at Thu Feb 08 05:48:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.