[CSHARP-4192] Compare a field in a BsonDocument to a string doesn't work Created: 01/Jun/22  Updated: 27/Oct/23  Resolved: 01/Jun/22

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

Type: Bug Priority: Unknown
Reporter: Dmitry Lukyanov (Inactive) Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CSHARP-3615

 Description   

           var result = Builders<BsonDocument>.Filter.Where(x => x["_id"] != "");
            var registry = BsonSerializer.SerializerRegistry;
            var serializer = registry.GetSerializer<BsonDocument>();
            var mql = result.Render(serializer, registry, Linq.LinqProvider.V3); 

The above code triggers exception:

MongoDB.Driver.Linq.ExpressionNotSupportedException: 'Expression not supported: (x.get_Item("_id") != ).' 

in this line https://github.com/mongodb/mongo-csharp-driver/blob/88769e2bd8866abd40c274552e38127400a0e731/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/StringExpressionToRegexFilterTranslator.cs#L305.



 Comments   
Comment by Robert Stam [ 01/Jun/22 ]

This is a false alarm. The following test passes:

[Fact]
public void Test()
{
    var filter = Builders<BsonDocument>.Filter.Where(x => x["_id"] != "");
 
    var documentSerializer = BsonDocumentSerializer.Instance;
    var serializerRegistry = BsonSerializer.SerializerRegistry;
    var rendered = filter.Render(documentSerializer, serializerRegistry, LinqProvider.V3);
 
    rendered.Should().Be("{ _id : { $ne : '' } }");
}

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