title: Set MongoDB to Serialize a class as BsonDocument
body: <p>I have a <code>Dictionary&lt;string, object&gt;</code> as an insertion input for <code>MongoDB.Save()</code>.</p>
  
  <p>When I fill this dictionary with primitive types - the serialization process works fine but when I'm trying to insert my dictionary filled with some custom class (for example - Person class) I'm getting the next error:
  <code>.NET type 'Person' cannot be mapped to a BsonValue</code></p>
  
  <p>I noticed that if I insert my custom type converted to <code>BsonDocument</code> the serialization process work great.</p>
  
  <p>How can I define MongoDB to serialize a particular class as <code>BsonDocument</code>?</p>
  
  <hr>
  
  <p>Update: real code provided</p>
  
  <pre><code>    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; }
      }
  </code></pre>
  
  <p>Here is the methods which saves the data to MongoDB:</p>
  
  <pre><code>public void Save(Dictionary&lt;string, object&gt; RawSensorMessageDictionary)
          {          
              try
              {
                  var policyColl = new MongoClient(ConnString).GetServer().GetDatabase(DB_NAME).GetCollection&lt;BsonDocument&gt;(CollectionNames.RawSensorMessage.ToString());
  
  
                  if (!RawSensorMessageDictionary.Any(p =&gt; 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
              }
          }
  </code></pre>
  
  <p>And here is the code from my UnitTest:</p>
  
  <pre><code>            DataAccess.MongoDAL dal = new DataAccess.MongoDAL();
              Dictionary&lt;string, object&gt; dic = new Dictionary&lt;string, object&gt;();
              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 );
  </code></pre>
  
  <p>I want my app to Serialize automatically <code>SensorData</code> object to <code>BsonDocument</code>. If I don't do it manually I'm getting the following exception: <code>.NET type 'SensorData' cannot be mapped to a BsonValue</code></p>
  
  <p>How should I do it?</p>
  
  <hr>
  
  <p>Update 2:
  OK found the issue, In my "save to db" method I used the next code in order to convert my dictionary  <code>BsonDocument</code>:</p>
  
  <pre><code>        var bsonObj = new BsonDocument(RawSensorMessageDictionary);
  </code></pre>
  
  <p>I would expect that creating a new instance of 'BsonDocument' from other object will handle the conversion exactly like <code>object.ToBsonDocument()</code> does but apparently it's not. </p>
  
  <p>Finally I replaced the code above to the following one:</p>
  
  <pre><code>    var bsonObj = RawSensorMessageDictionary.ToBsonDocument();
      dal.SaveRawSensorData(dic );
  </code></pre>
  
  <p>now it works. </p>