Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.BsonValueSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer'使用PullFilter刪除數組元素時出現異常
嘗試使用C#驅動程序(2.2.4或2.3.0)從MongoDB中的子文檔列表執行Pull操作。
這是我做了更新:
FilterDefinitionBuilder<Event> filter = new FilterDefinitionBuilder<Event>();
UpdateDefinitionBuilder<Event> update = new UpdateDefinitionBuilder<Event>();
_eventRepo.FindAndUpdate(filter.Eq("EventId", eventid),
update.PullFilter("Documents", filter.Eq("DocId", docid)));
被叫庫方法:
public void FindAndUpdate(FilterDefinition<T> filter, UpdateDefinition<T> update)
{
_context.Collection<T>().FindOneAndUpdate(filter, update);
}
這是MongoDB的文檔的樣子:
{
"_id" : ObjectId("5825f74919c55e0c9c4727ee"),
"EventId" : "1234-5789",
"Documents" : [
{
"DocId" : "07c03673-c572-4f56-aaad-0edb52b3a06c",
"Name" : "test.pdf"
}
]
}
這是我得到的例外:
An exception of type 'System.InvalidCastException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.BsonValueSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[MongoDB.Bson.BsonDocument]'.
Stack trace:
at MongoDB.Driver.PullUpdateDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOneAndUpdateOperation[TProjection](FilterDefinition`1 filter, UpdateDefinition`1 update, FindOneAndUpdateOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindOneAndUpdate[TProjection](FilterDefinition`1 filter, UpdateDefinition`1 update, FindOneAndUpdateOptions`2 options, CancellationToken cancellationToken)
異常沒有任何意義,因爲BsonValueSerializer
工具/繼承IBsonSerializer
(Documentation),所以我預計Render
應該能夠採取BsonValueSerializer
。
顯然這是在C#BSon驅動程序的FindOneAndReplace
方法中。這是司機的錯誤還是我做錯了什麼?
我發現那些人以同樣的方式拉(here),它似乎爲他們工作。 我發現看起來像一個非常類似的問題(here),但解決方案和討論無法幫助我修復我的問題。