title: How to custom mapping fields name or type for method MongoDB.Bson.Serialization.BsonSerializer.Deserialize
body: <p>I used the method MongoDB.Bson.Serialization.BsonSerializer.Deserialize() to deserialize from MongoDB.Bson.BsonDocument to MyType. But the method always meet System.FormatException since fields in MyType are not 100% match to the fields in BsonDocument.</p>
  
  <p>I've tried to convert a complex json object(let's called mobj) from MongoDB(query result) to C# object(let's called csobj), so that I could deal with the data. The defualt datatype in csobj I use is string. But the mobj is too complex and we know it's schema less.</p>
  
  <p>Once meet datatype like BinData(0,""), BinData(1,""), BinData(2,""), ISODate("") etc, in mobj, the System.FormatException may happen.</p>
  
  <p>Once there are extra new fields in mobj, the System.FormatException may happen.</p>
  
  <p>Once there are space in the field name like "Page one" : "XXXX", the the System.FormatException may happen and I don't know how to fix it till now.</p>
  
  <pre><code>var client = new MongoClient("mongodb://xxxxxx");
  var database = client.GetDatabase("xxxxxxxxxx");
  var collection = database.GetCollection&lt;BsonDocument&gt;("xxxxxxxxxx");
  
  var results = await collection.Aggregate&lt;BsonDocument&gt;(filterBsonDocumentArray).ToListAsync();
  
  foreach (var doc in results)
  {
      var model = MongoDB.Bson.Serialization.BsonSerializer.Deserialize&lt;MyType&gt;(doc); // always meet exception here
  }
  </code></pre>
  
  <p>Exception examples:</p>
  
  <p>(mongodb datatype could not map with string)</p>
  
  <p>System.FormatException: An error occurred while deserializing the Id property of class MongoQueryDemo.MyType: Cannot deserialize a 'String' from BsonType 'Binary'. ---> System.FormatException: Cannot deserialize a 'String' from BsonType 'Binary'.</p>
  
  <p>(_id in mongo could not found UserId in C# object auto)</p>
  
  <p>System.FormatException: Element '_id' does not match any field or property of class MongoQueryDemo.MyType.</p>
  
  <p>My questions are list here:</p>
  
  <ol>
  <li><p>Is there any way to tell the Deserializer, please be case insensitive;</p></li>
  <li><p>Is there any way to customize the mapping the field name from mobj to csobj, like define "_id" --> UserId, "Ip Addr" --> "IpAddr";</p></li>
  <li><p>Is there any way to customize the datatype, let the datatype BinData(0,""), BinData(1,""), BinData(2,""), ISODate("") are all could be convert into string without System.FormatException;</p></li>
  <li><p>Is there any way to dealing whole complex sub-object mapping to C# string regardless its fields? Since its dynamic in schema less mongodb and I could not predefine any unknown field in the sub-ojbects.</p></li>
  </ol>