[CSHARP-971] FindAndModify throws MongoCommandException instead of MongoDuplicateKeyException on key violations Created: 12/May/14  Updated: 04/Apr/15  Resolved: 04/Apr/15

Status: Closed
Project: C# Driver
Component/s: Error Handling
Affects Version/s: 1.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: René M. Andersen Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The FindAndModify method does not inspect server errors to see if the error is in fact a duplicate key error. Instead the error is re-thrown as a MongoCommandException, thus leaving it to the caller to inspect the exception and determine if it is a duplicate key error. The Exception thrown looks like:

MongoDB.Driver.MongoCommandException : Command 'findAndModify' failed: exception: E11000 duplicate key error index: MeterProfileRepositoryTest_635354843219333625.MeterProfile.$id dup key: { : "1234" } (response: { "value" : null, "errmsg" : "exception: E11000 duplicate key error index: MeterProfileRepositoryTest_635354843219333625.MeterProfile.$id dup key: { : \"1234\" }", "code" : 11000, "ok" : 0.0 })

The duplicate key can happen when using FindAndModify with a query using the "_id" field and some other field in combination with an "Update.Replace" and "upsert = true".


Generated at Wed Feb 07 21:38:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.