MongoDB App Services trigger works only when changing data directly in the database

I have a MongoDB App Services that of course is connected to the MongoDB Atlas Cluster.

In the App Services, we have defined a trigger that should be invoked when the status of an entity changes.

The problem is that the trigger is only invoked when we go into the Atlas Cluster Collections and change the value of that property directly in the database.

However, when the same property is changed within the app and synced to the App Services, the trigger is not invoked.

What are we missing?

Hi, I suspect that the issue you are running into is in the triggers configuration. Most importantly the MatchExpression and/or the Operation Type’s.

I would recommend selecting both Replace and Update as event types. Additionally, I think it is a good idea to remove the match expression from your trigger and print out the full event (use EJSON.Stringify()). I suspect you will see that perhaps the Change Event is not exactly how you are expecting it to look like and that the match expression is filtering it out.

@Tyler_Kaye thanks for getting back.

I will try it out. Though, it wasn’t very clear where should I put the EJSON.Stringify(). Should I put it instead of the match expression? Or should I put it inside the function that handles the change event?

Sorry about the confusion. I think you should put it in the function that handles the change event. Then when it fires, youll see the full change event and perhaps notice why your configuration for the match expression is not working properly.

Do you have a match expression by the way? If so, what is it?

Thanks Tyler

Yes, we do have a match expression. It’s set to this value

{"updateDescription.updatedFields":{"StatusValue":{"$numberInt":"1"}}}

StatusValue is an integer property on the entity on which we have the trigger.

Let me do the debugging and get back to you.

I’ve removed the match expression and the trigger started firing. I logged the changeEvent that’s coming to the trigger function and here’s it

{
  "txnNumber": 293,
  "lsid": {
    "id": {},
    "uid": {}
  },
  "_id": {
    "_data": "8263FF723C000000092B022C01002B046E5A100418F05FBB91AA496C8332E47CDB539C1E463C5F6964003C64633463343130612D663337632D343161312D623766342D303539656237306131643866000004"
  },
  "operationType": "update",
  "clusterTime": {
    "$timestamp": {
      "t": 1677685308,
      "i": 9
    }
  },
  "ns": {
    "db": "msoisales-dev-eu2-realm",
    "coll": "Move"
  },
  "documentKey": {
    "_id": "dc4c410a-f37c-41a1-b7f4-059eb70a1d8f"
  },
  "updateDescription": {
    "updatedFields": {
      "StatusValue": 1,
      "Description": "\r\nThe move is booked"
    },
    "removedFields": [],
    "truncatedArrays": []
  }
}

I’ve re-enabled the match expression:

{"updateDescription.updatedFields":{"StatusValue": 1}}

If we take a look at the JSON payload, the match expression should match the trigger, but unfortunately, it doesn’t. I see the same behaviour - when I change the field from the Atlas Collection directly, the trigger is firing. However, when I change the data from Realm app, it doesn’t fire for some reason.

I’ve also compared the JSON payload in both of the cases, and they are matching. Thus, my logical assumption would be that the problem is not in the match expression itself.

I have found the issue, though I am not sure how I can resolve it.

The difference between changing the field directly in the Atlas collection and the way the field is changed in the mobile app, is that in the database I change only that single field. In the mobile app, when I change the status to 1, I set the description of the move in the same transaction to "\r\nThe move is booked".

When I changed the business logic on the mobile app to set only the StateValue the match expression started working.

How can I update the match expression in a way, that it doesn’t care whether it was a single field that was updated or several of them? It should only care about the fact that the property I have specified has the value I have specified.

I believe you will want this to be:

{"updateDescription.updatedFields.StatusValue": 1}

The previous expression was looking for when the entire “updatedFields” was equal to the object "{ StatucValue: 1 }, but as you pointed out, when multiple values are changed that will not match. The match expression above should be what you are looking for, but let me know if it works for you. Match expressions are just a MongoDB Query on the Change Event document, so this should find any event where the StatusValue field is one of the fields that is updated and it is updated to 1

It did the job! Thanks a lot @Tyler_Kaye

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