title: Patch REST API to Partial Update MongoDB in .NET
body: <p>I have an object </p>
  
  <pre><code>{
    "_id": "testobject",
    "A": "First line",
    "B": "Second line",
    "C": "Third line"
  }
  </code></pre>
  
  <p>I want to send a REST PATCH request to my API to only update one of these properties</p>
  
  <pre><code>{
    "_id": "testobject",
    "C": "Forth line"
  }
  </code></pre>
  
  <p>This gets parsed into a class</p>
  
  <pre><code>public class SomeObject {
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
  }
  </code></pre>
  
  <p>I now need to update the existing document in MongoDB but only updating the property <code>C</code>.</p>
  
  <p>I could create an update definition just for this one record</p>
  
  <pre><code>UpdateDefinition&lt;SomeObject&gt; update = Builders&lt;SomeObject&gt;.Update.Set(x =&gt; x.C, &lt;value of property C&gt;)
  </code></pre>
  
  <p>Or I could hard code a check on each property to see if it is empty</p>
  
  <pre><code>IList&lt;UpdateDefinition&lt;SomeObject&gt;&gt; updates = new List&lt;UpdateDefinition&lt;SomeObject&gt;&gt;();
  if (!string.IsNullOrEmpty(C)) {
    updates.Add(UpdateDefinition&lt;SomeObject&gt; update = Builders&lt;SomeObject&gt;.Update.Set(x =&gt; x.C, &lt;value of property C&gt;));
  }
  if (!string.IsNullOrEmpty(C)) {
    updates.Add(UpdateDefinition&lt;SomeObject&gt; update = Builders&lt;SomeObject&gt;.Update.Set(x =&gt; x.C, &lt;value of property C&gt;));
  }
  </code></pre>
  
  <p>However, if I have many properties and many sub properties this could get very large very fast. The other issue is that if I set the value of the property to be intentionally empty then it would not update the record at all due to it looking for the field to non-empty.</p>
  
  <p>How can I dynamically do partial updates to MongoDB documents in .NET so that I have a generic PATCH API call that can take any of the parameters the document has and only update the properties specified?</p>