Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-971

FindAndModify throws MongoCommandException instead of MongoDuplicateKeyException on key violations

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.9
    • Component/s: Error Handling
    • None

      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".

            Assignee:
            Unassigned Unassigned
            Reporter:
            rem René M. Andersen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: