Debopam
Debopam

Reputation: 3356

MongoDB update field only if cursor has field

I am updating MongoDB document using a cursor loop. But in the below query cursor owner 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.

Even I tried db.contact_coolection.find({"_id" : ObjectId("5b876144d87b4d06ecb571b8")}) --> This gives ownerCRMContactId.$cond is not valid.

I think $cond is aggregator operator, what should be used in this case?

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: '' }
               }
        }
     });
});

Below is the sample document doesn't containing owner

{
    "_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"
        }
    ]
}

Upvotes: 1

Views: 216

Answers (1)

Ashh
Ashh

Reputation: 46461

You can do this way

const promises = db.contact_coolection.find().forEach(async(results) => {
  print( "Id: " + results._id);
  await db.contact_coolection.update(
    { _id: results._id },
    { $set: {
      ownerCRMContactId: results.owner ? results.owner.$id : ''
    }}
  )
})

await Promise.all(promises)

Update

const promises = db.contact_coolection.find().forEach(async(results) => {
  print( "Id: " + results._id);
  await db.contact_coolection.update(
    { _id: results._id },
    { $set: {
      ownerCRMContactId: (results && results.owner) ? results.owner.$id : ''
    }}
  )
})

await Promise.all(promises)

Upvotes: 2

Related Questions