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

Update with aggregation pipeline fails when used on a OfTypeMongoCollection (OfType<T>)

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 2.18.0
    • 2.17.1
    • CRUD
    • None

    Description

      Summary

      When trying to update documents with an aggregation pipeline this fails when used on a OfTypeMongoCollection that was aquired through OfType<T>(). The error message is: 

      'Unable to cast object of type 'MongoDB.Bson.BsonArray' to type 'MongoDB.Bson.BsonDocument'.'

      This happens independent of the pipeline stages; even an empty pipeline leads to this error. 

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      2.17.1, tested both on MongoDB 5.0.9 Community and ATLAS 5.0.10 Enterprise

      How to Reproduce

      var pipeline = new EmptyPipelineDefinition<DerivedClass>()
          .AppendStage<DerivedClass, DerivedClass, DerivedClass>(new BsonDocument
          {
              { 
                  "$set", 
                  new BsonDocument
                 

      {                 \{ nameof(DerivedClass.Value), "Pipeline" }

                  }
              }
          });
      var update = Builders<DerivedClass>.Update.Pipeline(pipeline);
      // Fails with: 'Unable to cast object of type 'MongoDB.Bson.BsonArray' to type 'MongoDB.Bson.BsonDocument'.'
      collDerived.UpdateOne(
          x => x.Key == "TestKey",
          pipeline,
          new UpdateOptions() { IsUpsert = true });

      Additional Background

      See attached .cs-file for a sample that demonstrates the error.

      Attachments

        Activity

          People

            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            m.wildgruber@sevantage.de Markus Wildgruber
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: