我有以下POCO類:MongoDb.Driver 2.4 deserilization數據類型改變
class MyClass {
public Objectid _id {get;set;}
public string property1 {get;set;}
public string property2 {get;set;}
public int property3 {get;set;}
}
的對象存儲在MongoDB的收藏。該數據在生成的BSON正確的數據類型:
property1: "SomeString"
property2: "12345"
property3: 98765
當我嘗試查詢集合:
var items = db.GetCollection<MyClass>("MyClass").AsQueryable().Select(x => x.property1 == "SomeString").ToList();
我得到指示property2錯誤不能被反序列化: 不能反序列化'字符串'從BsonType'Int64'
我想反序列化從數據庫中的bson文檔的字符串值到我的對象上的字符串值。
爲什麼BsonSerializer嘗試將其轉換爲十進制?在這種情況下,該值恰巧是數字,但該字段在類中定義爲字符串,因爲該值通常是字母數字。
我在VS2013中使用MongoDb.Driver v2.4軟件包。
字段'property2'中的集合'MyClass'包含類型爲int64的值。您可以直接在Mongo控制檯中使用javascript更新數據類型。我認爲c#驅動程序不支持相同的功能。 – rnofenko