[GODRIVER-2281] IsDuplicateKeyError does not work on individual bulk write errors Created: 24/Jan/22  Updated: 10/Feb/22  Resolved: 03/Feb/22

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: 1.8.2
Fix Version/s: 1.9.0

Type: Improvement Priority: Unknown
Reporter: Nicolas Nannoni Assignee: Benji Rewis (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed

 Description   

mongo.IsDuplicateKeyError is a convenient helper to detect duplicates. It however does not work when the duplicate key error is returned as part of a bulk. While it will tell whether any of the errors in the bulk was a duplicate, it can't be applied to the individual errors (of type BulkWriteError) within the BulkWriteException unless I missed a conversion somewhere.

_, err := collection.BulkWrite(ctx, ops, options.BulkWrite().SetOrdered(false))
if err != nil {
    berr := &mongo.BulkWriteException{}
    if !errors.As(err, berr) {
        return err
    }    
    for _, we := range berr.WriteErrors {
        // >>>> 
        // >>>> Does not work: 
        // >>>> mongo.IsDuplicateKeyError() does not understand either 'we' (BulkWriteError) or 'we.WriteError' (WriteError)
        // >>>>
        if mongo.IsDuplicateKeyError(we) {
            continue
        }
        return err
    }
    // The bulk only contains duplicate key errors: all good
} 

It's a bit limiting since it's hard to decide what do with a whole batch just knowing that at least one of the operations failed due to a duplicate key error. Iterating on the errors in the bulk should allow mongo.IsDuplicateKeyError to be applied to them and return true if relevant. It has a code and a message so the same logic as for serverError should be applicable.

 



 Comments   
Comment by Githook User [ 10/Feb/22 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: GODRIVER-2281 Document that collection enumeration is limited to 100 on 2.6 (#850)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/f11e0ad1efd05f9ab2d3d27890061f5787fd9520

Comment by Benji Rewis (Inactive) [ 03/Feb/22 ]

Implemented ServerError for WriteError. This improvement will be a part of version 1.9.0 of the Go driver.

Comment by Githook User [ 03/Feb/22 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: GODRIVER-2281 Implement ServerError for WriteError (#845)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/196daf88cd8a6b83ffb5cdccc4a85b9d4ee01eda

Comment by Nicolas Nannoni [ 02/Feb/22 ]

Great, thank you!

Comment by Benji Rewis (Inactive) [ 02/Feb/22 ]

Thanks again for your improvement suggestion, nicolas.nannoni@accedo.tv! WriteError does not implement ServerError, so the type cast in IsDuplicateKeyError does not work correctly. WriteErrors do, as you mentioned, contain both a Code and a Message , so implementing the interface should be easy enough. Here's a PR to do so.

Comment by Benji Rewis (Inactive) [ 01/Feb/22 ]

Hello nicolas.nannoni@accedo.tv! Thanks for your improvement suggestion; we're taking a look now.

Generated at Thu Feb 08 08:38:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.