2014-10-26 21 views
0

我有一個Dictionary<string, object>作爲MongoDB.Save()插入輸入。設置MongoDB序列化一個類作爲BsonDocument

當我填寫這本詞典與基本類型 - 序列化過程中工作正常,但當我試圖插入我的字典裏充滿了一些自定義的類(例如 - Person類),我發現了一個錯誤: .NET type 'Person' cannot be mapped to a BsonValue

我注意到,如果我插入我的自定義類型轉換爲BsonDocument序列化過程工作很好。

如何定義MongoDB以序列化一個特定的類爲BsonDocument


更新:真正的代碼提供

here is my SensorData class: 

    [DataContract] 
    public class SensorData 
    { 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.String)] 
     public string Type { get; set; } 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.String)] 
     public string Desc { get; set; } 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.String)] 
     public SensorStatus Status { get; set; } 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.String)] 
     public string Value { get; set; } 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.String)] 
     public string ValueUnits { get; set; } 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.Boolean)] 
     public bool Event { get; set; } 

     [DataMember] 
     [BsonElement] 
     [BsonRepresentation(BsonType.Int32)] 
     public int TTL { get; set; } 
    } 

這裏是一個將數據保存到MongoDB的方法:

public void Save(Dictionary<string, object> RawSensorMessageDictionary) 
     {   
      try 
      { 
       var policyColl = new MongoClient(ConnString).GetServer().GetDatabase(DB_NAME).GetCollection<BsonDocument>(CollectionNames.RawSensorMessage.ToString()); 


       if (!RawSensorMessageDictionary.Any(p => p.Key == "_id")) //if rawSensorMessageID is empty, mongodb will set it 
        RawSensorMessageDictionary.Add("_id", ObjectId.GenerateNewId().ToString()); 
       var bsonObj = new BsonDocument(RawSensorMessageDictionary); 
       policyColl.Save(bsonObj); 

      } 
      catch (Exception ex) 
      { 
        //log exception 
      } 
     } 

這裏是從我的單元測試代碼:

  DataAccess.MongoDAL dal = new DataAccess.MongoDAL(); 
      Dictionary<string, object> dic = new Dictionary<string, object>(); 
      dic.Add("1", true); 
      dic.Add(Message.RESOURCE_TYPE, "aaa"); 
      dic.Add(Message.RESOURCE_NAME, "bbb"); 
      dic.Add(Message.SENSOR_DATA, new SensorData { Desc = "aaa", Event = true, Status = SensorStatus.Active, TTL = 4, Type = "sss", Value = "222" }); 
      dal.SaveRawSensorData(dic); 

我想我的應用程序序列化一個自動SensorData反對BsonDocument。如果我不這樣做手動我收到以下異常:.NET type 'SensorData' cannot be mapped to a BsonValue

我應該怎麼做?


更新2: 確定發現的問題,在我的「保存到數據庫」的方法我用下面的代碼,以轉化我的字典BsonDocument

 var bsonObj = new BsonDocument(RawSensorMessageDictionary); 

我希望,創造一個來自其他對象的'BsonDocument'的新實例將完全像object.ToBsonDocument()那樣處理轉換,但顯然不是。

最後我換成上面的代碼爲以下之一:

var bsonObj = RawSensorMessageDictionary.ToBsonDocument(); 
    dal.SaveRawSensorData(dic); 

現在它的工作原理。

回答

0

如果字典中的字符串是您的mongodb文檔中的屬性,那麼非基元類型就是mongodb中的一個子文檔。請嘗試使用[BsonElement]和Bson Id與[BsonId]標記課程中的所有屬性。並確保您遵守所有的規則在這裏:Serialize Documents with the C# Driver 這將是很好,如果你提供一些更多的信息

編輯1 我會嘗試刪除除了價值SensorData所有屬性。如果這個工作,我會添加所有其他(一個接一個)。

+0

試過 - 沒有成功 – Ofir 2014-10-27 07:20:37

+0

你能提供更多的代碼嗎? – BendEg 2014-10-27 07:21:43

+0

還有一個問題,如果字典的一個實例是一個文檔,還是字典中的每個條目都是一個文檔? – BendEg 2014-10-27 07:34:50

相關問題