Description
Summary
Using LINQ3 If Project will produce a null result element an exception will be thrown:
_System.InvalidOperationException: ReadName can only be called when State is Name, not when State is EndOfDocument.
|
at MongoDB.Bson.IO.BsonReader.ThrowInvalidState(String methodName, BsonReaderState[] validStates)
|
at MongoDB.Bson.IO.BsonBinaryReader.ReadName(INameDecoder nameDecoder)
|
at MongoDB.Bson.IO.IBsonReaderExtensions.ReadName(IBsonReader reader)
|
at MongoDB.Bson.IO.IBsonReaderExtensions.VerifyName(IBsonReader reader, String expectedName)
|
at MongoDB.Bson.IO.IBsonReaderExtensions.ReadName(IBsonReader reader, String name)
|
at MongoDB.Driver.Linq.Linq3Implementation.Serializers.WrappedValueSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
|
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
|
at MongoDB.Driver.Core.Operations.CursorBatchDeserializationHelper.DeserializeBatch[TDocument](RawBsonArray batch, IBsonSerializer`1 documentSerializer, MessageEncoderSettings messageEncoderSettings)
|
at MongoDB.Driver.Core.Operations.FindOperation`1.CreateFirstCursorBatch(BsonDocument cursorDocument)
|
at MongoDB.Driver.Core.Operations.FindOperation`1.CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument commandResult)
|
at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
|
at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
|
at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
|
at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
|
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
|
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)_
|
LINQ2 adds null elements in the result.
How to Reproduce
define a class with a nullable type property
class TestData |
{
|
[BsonId]
|
[BsonRepresentation(BsonType.ObjectId)]
|
public string _id { get; set; } |
public double? DoubleNullableValue { get; set; } |
}
|
Insert elements with null value for DoubleNullableValue
|
_var mongoUrl = MongoUrl.Create("mongodb://localhost:27017");
var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
clientSettings.LinqProvider = LinqProvider.V3;
var mongoC = new MongoClient(clientSettings);
var collection = mongoC.GetDatabase("Tests").GetCollection<TestData>("NullableDataTest");
//Insert test samples
await collection.InsertManyAsync(new []
{
new TestData
,
new TestData()
});_
Make a find thath will not exlude the null values and projects them
__
await collection.Find(_ => true).Project(t => t.DoubleNullableValue).ToListAsync(); |
The above exception will be thrown.
Additional Background
.NET6 Console project reproducing bug attached. (Ensure {}mongoUrl).