I have a MongoDB collection ("Users") which holds a dictionary field ("UserRegistrations").
The field definition is:
    BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
    public Dictionary<string, UserRegistration> UserRegistrations = new Dictionary<string, UserRegistration>();
It's a dictionary which consists of a key (String) and a customized object as value.
This is how it reflects in MongoDB:
"UserRegistrations" : [{
  "k" : "517ba4e1696b03108ccef51a",
  "v" : {
    "RegistrationDate" : ISODate("2013-07-21T18:57:42.589Z"),
    "OtherInfo" : "123456test",
  }
}],
When I use $AddToSet, as following example:
IMongoQuery query = Query.EQ("_id", new ObjectId(uid));
var kvp = new KeyValuePair<string, UserRegistration>("517ba4e1696b03108ccef51a", new UserRegistration()
{
 RegistrationDate = DateTime.Now.ToUniversalTime(),
});
IMongoUpdate update = Update.AddToSet("UserRegistrations", kvp.ToBsonDocument());
collection.Update(query, update, UpdateFlags.Multi);
It doesn't check if the same key exists already, causing a duplication in a Dictionary field which later causes deserialization errors in MongoDB C# driver.
How can I make sure the same key doesn't exists already?
Thanks!
 
     
    