Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4538

Error on Project if null values are not filtered

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Unknown Unknown
    • 2.19.1
    • 2.19.0
    • LINQ3
    • None
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

    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

      { DoubleNullableValue = 1.4d }

      ,
      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).

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            xsaf94x@gmail.com Salvo Furnari
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: