[CSHARP-4293] Update with aggregation pipeline fails when used on a OfTypeMongoCollection (OfType<T>) Created: 11/Aug/22  Updated: 28/Oct/23  Resolved: 25/Aug/22

Status: Closed
Project: C# Driver
Component/s: CRUD
Affects Version/s: 2.17.1
Fix Version/s: 2.18.0

Type: Bug Priority: Major - P3
Reporter: Markus Wildgruber Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File Program.cs    
Issue Links:
Related
is related to CSHARP-4745 Unable to cast object of type 'MongoD... Closed

 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.



 Comments   
Comment by Githook User [ 25/Aug/22 ]

Author:

{'name': 'Dmitry Lukyanov', 'email': 'dmitry.lukyanov@mongodb.com', 'username': 'DmitryLukyanov'}

Message: CSHARP-4293: Update with aggregation pipeline fails when used on a OfTypeMongoCollection (OfType<T>). (#869)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/786e32440a3a14b1125a1e98e4495ac38bbc9b3b

Comment by Dmitry Lukyanov (Inactive) [ 15/Aug/22 ]

m.wildgruber@sevantage.de, thanks for your report, I can confirm that it's a bug. We've scheduled this work, please follow to this ticket for updates.

Comment by Markus Wildgruber [ 12/Aug/22 ]

The problem was discovered when trying to perform a BulkWriteAsync on the OfTypeMongoCollection. This is not part of the sample code. Please also check BulkWriteAsync with UpdateOnModels that are based on an aggregation pipeline. 

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