[CSHARP-691] UpdateBuilder.Set throws InvalidOperationException when Set twice with the same element. Created: 28/Feb/13  Updated: 06/Oct/16  Resolved: 06/Oct/16

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.7.1
Fix Version/s: 2.0

Type: Bug Priority: Major - P3
Reporter: Prasanna V. Loganathar Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB C# Driver.


Issue Links:
Depends
depends on CSHARP-474 Review all exceptions thrown by the C... Closed
Duplicate
is duplicated by CSHARP-902 UpdateBuilder.Set should overwrite va... Closed
Related

 Description   

UpdateBuilder.Set throws InvalidOperationException when Set twice with the same element.

Eg:

UpdateQuerySet.Set("Email", value1);
UpdateQuerySet.Set("Email", value2);

Ideally, it should have overwritten the first value. Or even, as a worst case, it should just add the second value along, since update({},

{"email": "value1", "email" : "value2"}

works on perfectly as a command when given directly using the MongoDB shell.



 Comments   
Comment by Robert Stam [ 06/Oct/16 ]

The UpdateBuilder in the 1.x driver uses the rule that a duplicate Set throws an exception.

The new UpdateDefinitionBuilder in the 2.x driver uses the rule that the last Set wins.

At this point we should just leave the 1.x API alone, and since the the 2.x builder no longer throws an exception I'm going to close this as Fixed in 2.0.

Comment by Sridhar Nanjundeswaran [ 03/Jun/13 ]

Note the server does not allow multiple modifiers on the same field

> db.foo.find()
{ "_id" : 1 }
{ "_id" : 2, "v" : 2 }
 
> db.foo.update({_id:2}, {$set:{v:1},$inc:{v:1}})
Field name duplication not allowed with modifiers

The driver will not allow setting the same element twice. Having said that we will change the exception thrown to be ArgumentException. This is more in tune with .Net throwing ArgumentException on a duplicate on Dictionary.Add http://msdn.microsoft.com/en-us/library/k7z0zy8k.aspx.

Comment by Sridhar Nanjundeswaran [ 01/Jun/13 ]

As far as what the shell does is that it basically takes the last value. In your example email would be value 2. This is same behavior you see in some of the other drivers such as PHP and Ruby and this is because what you are passing in is an array/hash/dictionary. Here the repeated key just results in the last value being taken.
Since we are dealing with a higher level API here we are being explicit about the semantic

Comment by Craig Wilson [ 16/May/13 ]

Perhaps a better exception type is all that is necessary? DuplicateKeyException or something?

I don't think adding the key to the document twice is a good option. So that leaves us with two options:

  1. Overwrite the original value
  2. Throw an exception

Overwriting a value could easily lead to bugs in your application that could be very difficult to track down. "I've set the value of Email to XXX very explicitly here, yet it is getting set to YYY" would be the common complaint. Whereas, when we throw the exception, you know very specifically where that is happeneing and can take action to mitigate very easily.

If you feel strongly about this, then please write back and give us your thoughts. For now, I'm going to mark this for a fix in 1.9 to change the exception getting thrown.

Generated at Wed Feb 07 21:37:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.