[CSHARP-1975] Change of BsonNull.Value in c# driver from Version 2.2 to 2.4.3 Created: 02/May/17  Updated: 27/Oct/23  Resolved: 03/May/17

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

Type: Task Priority: Blocker - P1
Reporter: Dheeraj kumar Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: Bug, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

I am using BsonValue.Null for querying in filter
I am creating my filter like this:
Builders<T>.Filter.Eq("TransactionKey", BsonNull.Value) | Builders<T>.Filter.Eq("TransactionKey", string.Empty)

In Version 2.2 this query is rendered as
{ "$or" : [

{ "TransactionKey" : null }

,

{ "TransactionKey" : "" }

] }

but in Version 2.4.3 this query is rendered as
{ "$or" : [

{ "TransactionKey" : "BsonNull" }

,

{ "TransactionKey" : "" }

] }

So is the value of BsonValue.Null is changed from null to "BsonNull" purposely or is it a defect?



 Comments   
Comment by Githook User [ 17/May/17 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1982: Mention CSHARP-1975 in the release notes for 2.4.3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f4a3ead5a2cbbb543ae592b0cde87d368c15dd91

Comment by Robert Stam [ 03/May/17 ]

This is a side effect of some improvements we have made to how constant values are serialized when building a query. We now attempt to convert the supplied constant value to the type of the field so that we can use the field's serializer to serialize the constant value. This is specially important when the field has a serializer that serializes values in a non-standard way.

Here's an explanation of what's happening. When you write:

builder.Eq("TransactionKey", BsonNull.Value);

the compiler is inferring the type of <TField> from the supplied value. So that's actually equivalent to:

builder.Eq<BsonNull>("TransactionKey", BsonNull.Value);

But... the actual field is of type string, not BsonNull. So we now convert the BsonNull constant to a string, and that's where the "BsonNull" string is coming from.

The easy workaround is to use null instead of BsonNull as the value:

builder.Eq<string>("TransactionKey", null);

Note: you have to supply the <string> type parameter because the compiler can't infer a type from null.

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