title: Serializing C# classes to MongoDB without using discriminators in subdocuments
body: <p>I'm writing C# code that writes to a Mongo database used by an existing Web app (written in PHP), so I need to not change the existing structure of the database. The database structure looks something like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
      "_id": ObjectId("5572ee670e86b8ec0ed82c61")
      "name": "John Q. Example",
      "guid": "12345678-1234-5678-abcd-fedcba654321",
      "recordIsDeleted": false,
      "address":
      {
          "line1": "123 Main St.",
          "city": "Exampleville"
      }
  }
  </code></pre>
  
  <p>I read that in to a class that looks like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Person : MongoMappedBase
  {
      public ObjectId Id { get; set; }
      public Guid Guid { get; set; }
      public bool RecordIsDeleted { get; set; }
      public string Name { get; set; }
      public AddressData Address { get; set; }
      // etc.
  }
  
  public class AddressData : MongoMappedBase
  {
      public string Line1 { get; set; }
      public string City { get; set; }
      // etc.
  }
  </code></pre>
  
  <p>The reading code looks like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var collection = db.GetCollection&lt;Person&gt;("people");
  List&lt;Person&gt; people = collection.Find&lt;Person&gt;(_ =&gt; true).ToListAsync().Result;
  </code></pre>
  
  <p>(Note: I'm still in development. In production, I'm going to switch to <code>ToCursorAsync()</code> and loop through the data one at a time, so don't worry about the fact that I'm pulling the whole list into memory.)</p>
  
  <p><strong>So far, so good.</strong></p>
  
  <p>However, when I write the data out, this is what it looks like:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
      "_id": ObjectId("5572ee670e86b8ec0ed82c61")
      "name": "John Q. Example",
      "guid": "12345678-1234-5678-abcd-fedcba654321",
      "recordIsDeleted": false,
      "address":
      {
          "_t": "MyApp.MyNamespace.AddressData, MyApp",
          "_v":
          {
              "line1": "123 Main St.",
              "city": "Exampleville"
          }
      }
  }
  </code></pre>
  
  <p>Notice how the <code>address</code> field looks different. <strong>That's not what I want.</strong> I want the address data to look just like the address data input (no <code>_t</code> or <code>_v</code> fields). In other words, the part that ended up as the <em>contents</em> of <code>_v</code> is what I wanted to persist to the Mongo database as the value of the <code>address</code> field.</p>
  
  <p>Now, if I was just consuming the Mongo database from my own C# code, this would probably be fine: if I were to deserialize this data structure, I assume (though I haven't yet verified) that Mongo would use the <code>_t</code> and <code>_v</code> fields to create instances of the right type (<code>AddressData</code>), and put them in the <code>Address</code> property of my <code>Person</code> instances. In which case, everything would be fine.</p>
  
  <p>But I'm sharing this database with a PHP web app that is <em>not</em> expecting to see those <code>_t</code> and <code>_v</code> values in the address data, and won't know what to do with them. I need to tell Mongo "Please do <strong><em>not</em></strong> serialize the type of the <code>Address</code> property. Just assume that it's always going to be an <code>AddressData</code> instance, and just serialize its contents without any discriminators."</p>
  
  <p>The code I'm currently using to persist the objects to Mongo looks like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public UpdateDefinition&lt;TDocument&gt; BuildUpdate&lt;TDocument&gt;(TDocument doc) {
      var builder = Builders&lt;TDocument&gt;.Update;
      UpdateDefinition&lt;TDocument&gt; update = null;
      foreach (PropertyInfo prop in typeof(TDocument).GetProperties())
      {
          if (prop.PropertyType == typeof(MongoDB.Bson.ObjectId))
              continue; // Mongo doesn't allow changing Mongo IDs
          if (prop.GetValue(doc) == null)
              continue; // If we didn't set a value, don't change existing one
          if (update == null)
              update = builder.Set(prop.Name, prop.GetValue(doc));
          else
              update = update.Set(prop.Name, prop.GetValue(doc));
      }
      return update;
  }
  
  public void WritePerson(Person person) {
      var update = BuildUpdate&lt;Person&gt;(person);
      var filter = Builders&lt;Person&gt;.Filter.Eq(
          "guid", person.Guid.ToString()
      );
      var collection = db.GetCollection&lt;Person&gt;("people");
      var updateResult = collection.FindOneAndUpdateAsync(
          filter, update
      ).Result;
  }
  </code></pre>
  
  <p>Somewhere in there, I need to tell Mongo "I don't care about the <code>_t</code> field on the <code>Address</code> property, and I don't even want to see it. I know what type of objects I'm persisting into this field, and they'll always be the same." But I haven't yet found anything in the Mongo documentation to tell me how to do that. Any suggestions?</p>