Database trigger not updating document in another collection

Hi, I have a database trigger that watches for a change in the cancelation status and then go to another collection to update that same field. The trigger is working and the console.log is giving me the updated document but when I query the collection to see if the document was updated the information is not change.

This is the first time I create a database trigger so I followed the snippet example to configure it.

The document field that I want to update is inside an object that is inside in a subarray. The schema is:

"report": [
        {
            "contract": [
                {
                    "latestVersion": {
                        "Client": "company name",
                        "cancelation": {
                            "updated": {
                                "$date": {
                                    "$numberLong": "1641814200000"
                                }
                            },
                            "status": true
                        },
                    "consecutive": "WRF-X-7786",
                }

So what I’m doing is:

return cancelStatus.aggregate([
      {
        '$unwind': {
          'path': '$report'
        }
      }, {
        '$unwind': {
          'path': '$report.contract'
        }
      }, {
        '$unwind': {
          'path': '$report.contratos.latestVersion'
        }
      }, {
        '$match': {
          'report.contratos.consecutive': <consecutive updated> 
        }
      }, {
        '$set': {
          'report.contract.latestVersion.cancelation.updated': <new datetime>, 
          'report.contract.latestVersion.cancelation.status': <new status>
        }
      }
    ]).next()
    .then(updateCancel => {
      console.log("Cancelation updated: " + JSON.stringify(updateCancel));
    })
    .catch(err => console.error("Failed to update cancelation status:", err));

I check the aggregation query in Compass and it works, also the console log gives me an succesful transaction, but the document is not updated and keeps the old values.

Can somebody please help me to understand where might be the error?

What is contractos?

Should it be contract like everywhere else?

yes, sorry is contracts, I translated from spanish but I forgot those.

At the first look the aggregation doesn’t have a $out or $merge stage to commit the data.

p.s $out will replace collection so try $merge.

Thanks for your help. I tried $merge, it updated the information but also deleted the rest of the objects in the document.

I finally used a convencional updateOne to make it work:

return cancelStatus.updateOne(
      {
        "report.contracts.consecutive": <value>
      }, {
        '$set': {
          'report.$[].contracts.$[].latestVersion.cancelation.updated': <new value>, 
          'report.$[].contracts.$[].latestVersion.cancelation.status': <new value>
        }
      }
    );

Thanks for your comments.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.