title: Increment nested field value in mongodb if exists or create nested fields
body: <p>I need to increment the multi level nested field value if it exists or create the complete nested field Object structure.</p>
  
  <p>Structure of my document</p>
  
  <pre><code>Doc1 {
  _id:ObjectId(),
  myField: {
    nested:{
        x: 5,
        y: 10,
        z: 20
    }
   }
  }
  </code></pre>
  
  <p><strong>Goal Explanation</strong>: I need a way to write a single query: </p>
  
  <ul>
  <li><strong>If myField exists</strong>: Increment the value of my nested field
  <strong>myField.nested.x</strong> by 10. </li>
  <li><strong>If myField does not exists</strong>:  Create the below field with initial values same as given in the <strong>Doc1</strong>.</li>
  </ul>
  
  <p><strong>Attempt and explanation</strong>: </p>
  
  <pre><code> db.collection('collectionName').findOneAndUpdate(
      {_id:"userId","myField" : { $exists : true }},
      {$inc:{'myField.nested.x':10}
   })
  </code></pre>
  
  <p>This way, I can  increment the nested field if it exists but in case of non existence I cannot set <em>myField</em> as same as Doc1. </p>
  
  <p>Although, I can use another query after response in my NodeJs callback to achieve my required behaviour. But I need some elegant solution in  a single query.</p>
  
  <p>I am using MongoDB version 4.0.4, Thanks in Advance.</p>