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

Time:06-16

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:

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
}

CodePudding user response:

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.

  • Related