0
votes

I don't understand why I am getting a duplicate key exception on Save. I thought the point of save was to update if there, insert if not. Here is the error.

WriteConcern detected an error 'E11000 duplicate key error index: cms.BaseVariables.$id dup key: { : "8f69cb40ab3568957c237ef360d29964" }'. (Response was { "err" : "E11000 duplicate key error index: cms.BaseVariables.$id dup key: { : \"8f69cb40ab3568957c237ef360d29964\" }", "code" : 11000, "n" : 0, "connectionId" : 6969, "ok" : 1.0 }).

Yes it is right, 8f69cb40ab3568957c237ef360d29964 is there already. So why is it not just updating it?

I have the following class map registration and ID property on the object...

BsonClassMap.RegisterClassMap<BaseVariableGroup>(cm =>
{
    cm.AutoMap();
    cm.SetIdMember(cm.GetMemberMap(c => c.Id));
    cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
});

public string Id
{
    get { return _id; }
    set
    {
        _id = value;
        _id = Md5Cryptography.Hash(string.Concat(SportId, CompetitionId, Round));
    }
}

And this is how I am calling Save...

_collection.Save(baseVariableGroup)

The exception...

enter image description here

1
This looks like C# but what language is this? - Sammaye
I've updated. You were correct. - Matt Canty
I can't reproduce the problem. Which version of the driver and which database version are you using? - mnemosyn
Actually I'm not sure if it's just normal behaviour when Common Language Runtime Exceptions is turned on... DB version 2.2.3 and driver version 1.7.0.4714. - Matt Canty

1 Answers

1
votes

I don't believe this is normal behavior for save, and I'm pretty sure you're eliciting it with how you generate the id (I think the driver believes the Id changes every time). There's is a much happier path which I'm sure will avoid this problem: create a POCO to group your "compound id" members public class BaseVariableGroup { public CompoundId Id { get; set: } }. You no longer need to touch the class map.