[CSHARP-356] Atomic update (Set) not work if object value null Created: 17/Nov/11 Updated: 02/Apr/15 Resolved: 18/Feb/12 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.3 |
| Fix Version/s: | 1.4 |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | Andrew Orsich | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 1 |
| Labels: | c#, driver | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
Example: var message = new SomeClassWithFieldCode(); I suppose driver should replace null with BsonNull.Value internally rather than set it manually on each update in case if some property value equals to null. Thank you. |
| Comments |
| Comment by Mark Walsh [ 03/Sep/13 ] |
|
Just incase you didn't get notified on Stackoverflow Robert. Can I ask why this throws an Argument Null Exception when the value is null? Rather than just default to BsonNull.Value when the value is null. I am refactoring after a driver update and this is killing me (it never used to throw an exception). |
| Comment by Robert Stam [ 18/Feb/12 ] |
|
All Update builder methods now throw an ArgumentNullException when called with an invalid null argument. |
| Comment by Robert Stam [ 18/Feb/12 ] |
|
Nulls are handled slightly differently when you are in the object mapping world vs the object model (BsonDocument) world. A C# null is mapped by the serialization layer to a BsonNull.Value, but C# null is not a valid BsonValue. So in our example above, the Code property is C# null in your data model and will be mapped to BsonNull.Value in your database. |
| Comment by Andrew Orsich [ 21/Nov/11 ] |
|
But when you writing Update.Set you don't want to omit this value, you want update it. In case of XDocument you always make update of entire document. message.Code = null; Also, is there any way to control this behavior via some settings? |
| Comment by Robert Stam [ 18/Nov/11 ] |
|
Null is never automatically converted to BsonNull.Value because there are many cases where null means to omit the item (it is part of the rules of functional construction of BsonDocuments, which by the way works the same way in .NET's XDocument classes). The intended way to handle null in your sample case is: var update = Update.Set("Code", (BsonValue) message.Code ?? BsonNull.Value); |