Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MongoDB says "duplicate key" for index field but it isn't

So I have a simple collection with an additional index on one field called "reactionId". I then want to bulk-upsert multiple documents to that collection. The first one gets inserted, and then I get the error

A bulk write operation resulted in one or more errors. WriteErrors: [ { Category : "DuplicateKey",
Code : 11000, Message : "E11000 duplicate key error collection: genetica.Reactions index:
reactionID_1 dup key: { : null }" } ].

I don’t see why the DB is reporting a duplicate key when they clearly are different?
MongoDB version is 4.0.26

This is the Reaction model:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Reaction
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    [BsonIgnoreIfNull]
    public ObjectId? id { get; set; }
    public string reactionId { get; set; } = "";
}

The insert operation:

public async Task UpsertReactions()
{
    var reactions = new List<Reaction>()
    {
        new(){ reactionId = "1234" },
        new(){ reactionId = "4321" }
    };
    
    var bulk = new List<WriteModel<Reaction>>();
    reactions.ForEach(newReaction =>
    {
        var upsertSingle = new ReplaceOneModel<Reaction>(
            Builders<Reaction>.Filter.Where(reaction => reaction.reactionId == newReaction.reactionId),
            newReaction) { IsUpsert = true };
        bulk.Add(upsertSingle);
    });

    await _reactions.BulkWriteAsync(bulk);
}

And the index is defined like this:

{ 
    "v" : 2, 
    "unique" : true, 
    "name" : "reactionID_1", 
    "ns" : "genetica.Reactions", 
    "background" : true
}

>Solution :

The problem is that the index is defined on the property "reactionID" – notice the upper-case ID. In your model, the property is named "reactionId" with a lower-case d. Hence, when inserting, your document does not contain a value for the property that MongoDB expects. This leads to a null value for both documents in the index – hence the duplicate key violation.

If you drop the index and redefine it for "reactionId", this insert should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading