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
}
}
});
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.
- is related to
-
CSHARP-4745 Unable to cast object of type 'MongoDB.Bson.BsonArray' to type 'MongoDB.Bson.BsonDocument'
- Closed