0

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

4
  • This looks like C# but what language is this? Commented Sep 10, 2013 at 13:04
  • I've updated. You were correct. Commented Sep 10, 2013 at 13:10
  • I can't reproduce the problem. Which version of the driver and which database version are you using? Commented Sep 10, 2013 at 13:30
  • 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. Commented Sep 10, 2013 at 15:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.