title: MongoDB update field only if cursor has field
body: <p>I am updating MongoDB document using a cursor loop. But in the below query cursor <strong><em>owner</em></strong> field cannot exist in the results. I am using below query but looks like results.hasOwnProperty('owner') always returning true and it is throwing error results.owner doesn't exists.</p>
  
  <p>Even I tried db.contact_coolection.find({"_id" : ObjectId("5b876144d87b4d06ecb571b8")}) --> This gives ownerCRMContactId.$cond is not valid.</p>
  
  <p>I think $cond is aggregator operator, what should be used in this case?</p>
  
  <pre><code>db.contact_coolection.find().forEach(function(results)
  {    
      print( "Id: " + results._id);
      db.contact_coolection.update( {_id : results._id},
      {$set : {
          "ownerCRMContactId": {
                   $cond: { if: results.hasOwnProperty('owner'), then: results.owner.$id, else: '' }
                 }
          }
       });
  });
  </code></pre>
  
  <p>Below is the sample document doesn't containing owner</p>
  
  <pre><code>{
      "_id" : ObjectId("5b876144d87b4d06ecb571b8"),
      "_class" : "com.cfcf.crm.model.auth.CRMContact",
      "crmId" : -09898,
      "prefix" : "Mr",
      "firstName" : "fghh",
      "middleName" : "asdgasd",
      "lastName" : "asdasd",
      "suffix" : "asdassd",
      "nickName" : "asdasd",
      "gender" : "Male",
      "age" : "0",
      "dlNumber" : "0",
      "height" : 0,
      "weight" : 0,
      "isSmoker" : false,
      "deleted" : false,
      "isEnabled" : false,
      "externalSource" : [],
      "familyMembers" : [],
      "crmInfos" : [ 
          {
              "opportunityId" : "5b95fa6c28e76b60c0454a2e"
          }
      ],
      "createdDate" : ISODate("2018-08-30T03:15:16.181Z"),
      "lastModifiedDate" : ISODate("2018-09-10T05:00:28.627Z"),
      "createdBy" : "5b2f43e433d58d3e0cd15304",
      "lastModifiedBy" : "5b2f43e433d58d3e0cd15304",
      "owner" : {
          "$ref" : "contact_coolection",
          "$id" : ObjectId("5b2f43e433d58d3e0cd15303")
      },
      "roles" : [],
      "links" : [ 
          {
              "linkId" : 0,
              "linkTitle" : "testsLinkss",
              "linkUrl" : "www.google.com"
          }
      ],
      "addresses" : [ 
          {
              "addressLine1" : "3rd Cross",
              "addressLine2" : "Kensington Street",
              "city" : "Birmingham",
              "state" : "Alabama",
              "country" : "US"
          }
      ]
  }
  </code></pre>