title: MongoDB Query, update specific field if value is not null
body: <p>I have an object <strong>"input"</strong> and will be passed to query like a parameter:</p>
  
  <pre><code>var input = {
      id: 1,
      componentId: x,
      commands: [...],
      user: [...],
  }
  </code></pre>
  
  <p>And the collection <strong>"testCollection"</strong>:</p>
  
  <pre><code>testCollection = {
      id: 1,
      model: {
          stepComponents: [
             {  
              componentId: x
              command: [...],
              user: [...],
             }
           ]
      }
  }
  </code></pre>
  
  <p>My question is how can I update a specific field on <strong>"testCollection"</strong> and skip the update if it is undefined or null.
  That means if <strong>"input.user"</strong> is undefined/null, then not update <strong>"user"</strong> field.</p>
  
  <p>The update query sample is below:</p>
  
  <pre><code>testCollection.update(
    {
      _id: objectId(input.id),
      'model.stepComponents.componentId': input.componentId
    },
    {
      'inputs': input.inputs,
      'model.stepComponents.$.commands': input.commands,
      'model.stepComponents.$.users': input.users,
    },
    { upsert: true },
  );
  </code></pre>
  
  <p>Any help would be appreciated. Thanks.</p>