title: Set subproperty on potentially null property in MongoDB in C#
body: <p>With MongoDB, using the C# driver, I need to construct an update for the property <code>x</code></p>
  
  <ul>
  <li>that sets <code>x.y</code> to 42</li>
  <li>that works if <code>x</code> is null</li>
  <li>and that also works if <code>x</code> is already set to an object with other properties (in which case I need those properties to be retained).</li>
  </ul>
  
  <p>In other words, I need to construct an update for the property <code>x</code> that sets <code>x.y</code> to 42 and that works simultaneously on the following two input documents:</p>
  
  <pre><code>{
    "x": null
  }
  
  should become
  
  {
    "x": {
      "y": 42
    }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
    "x": {
      "z": 66
    }
  }
  
  should become
  
  {
    "x": {
      "y": 42,
      "z": 66
    }
  }
  </code></pre>
  
  <p>The naïve approach of doing</p>
  
  <pre><code>Builders&lt;BsonDocument&gt;.Update.Set("x.y", 42)
  </code></pre>
  
  <p>results in the error message</p>
  
  <pre><code>cannot use the part (x of x.y) to traverse the element ({x: null})
  </code></pre>