[CSHARP-902] UpdateBuilder.Set should overwrite value if called twice with the same field Created: 24/Jan/14  Updated: 20/Mar/14  Resolved: 24/Jan/14

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

Type: Improvement Priority: Major - P3
Reporter: Amith George Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-691 UpdateBuilder.Set throws InvalidOpera... Closed

 Description   

Refer StackOverflow question (http://stackoverflow.com/q/21227428/30007) for detailed description. The answer to that question also suggests a workaround in the meanwhile.
----------

var update = new UpdateBuilder<SomeClass>();
update.Set(x => x.Prop1, value1);
update.Set(x => x.Prop2, value2);
update.Set(x => x.Prop1, anotherValue1); // this throws a Duplicate element name error. It should overwrite the value of Prop1 with anotherValue1.



 Comments   
Comment by Robert Stam [ 24/Jan/14 ]

See original ticket CSHARP-691 instead.

Comment by Robert Stam [ 24/Jan/14 ]

This ticket is a duplicate. Linking to the original ticket and resolving this one.

Comment by Robert Stam [ 24/Jan/14 ]

The server only allows one update operator per field. The current implementation of the Update builder makes no attempt to enforce this (perhaps it should?). If you apply multiple different update operators to the same field you will get an error server side. If you apply the same update operator (in your case Set) to the same field you get an exception client side, as a side effect of BsonDocument not allowing duplicate element names.

So the options here are:

1. Allow Set to be called multiple times (and last one wins)
2. Enforce client side the same restrictions the server does

Either way, whatever solution is adopted should be generalized to all update operators, not just Set.

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