Realm database trigger match expression seemingly not working

I have a realm function built firing off a database trigger. I need to add a match expression to filter the trigger, otherwise it will run on a continuous loop because it is updating the same collection that triggers it.

So my match expression is this:
{“updateDescription.updatedFields.profile”:{"$exists":true}}

When I go into my collection and update a document’s “profile” field, nothing happens. When I take away the match expression, it runs all the time, but it runs in a continuous loop like mentioned before.

Any ideas? Thanks!

Hi @Lukas_deConantseszn1,

The first qoutes in the statement looks malformed:

{“updateDescription.updatedFields.profile”:{"$exists":true}}

If there is an issue in parsing this the filtering might be off. Please fix as follows:

{
  "updateDescription.updatedFields.profile": {
    "$exists": true
  }
}

If this do not help please send us a link of the trigger.

Best
Pavel

1 Like

Hi Pavel, it seems that something is off with the way the match gets pasted In reality, the quotes are just fine in the match statement. I tried to fix them, but they are already set to the correct character type.

Here is a link:
https://realm.mongodb.com/groups/5bc1648acf09a2891bf25a98/apps/5e74024322411824cc3e0e1d/triggers/5f159a2084af949839f28c89

Thank you so much for your help!

@Pavel_Duchovny what do you think?

Hi @Lukas_deConantseszn1,

The logic is complex involving external dependency and I do not know the document type.

However, your code only set a field name recommendations, how do you expect the profile field to vanish from the document??

To me it looks like a classic infinity loop which has no exit condition.

Please explain.

Best
Pavel

So the inifinite loop issue is exactly why I need the match statement

Basically, if the profile field is updated, I want the trigger to run.

The trigger will run a function that will update the recommendations field, so that’s why I need the match statement to filter that update out. If recommendations is updated, or any change event that does not include an update to profile, I don’t want the function to run. But if the event includes profile, I do want it to run.

That way, when the function updates recommendations and does not update profile, the trigger will not get run, and there will be no infinite loop.

Best,
Lukas

Hi @Lukas_deConantseszn1,

Have you tried logging the updateDescription.updatedFields values so that you see there is no profile update but the trigger is still there?

Have you tried disable and re-enable the trigger?

Best
Pavel

Hi @Pavel_Duchovny,

After doing a lot of logging, I noticed something. For context, I have been testing by updating the document from within the Atlas UI. Apparently, doing so triggers a replace operation? Didn’t know that but, very interesting.

Anyway, once I figured that out I was able to implement the logic needed to only fire my function when I want. Thanks so much for your help and the great idea!

Hi @Lukas_deConantseszn1,

Yes updates in Data Explorer are basically replace commands.

I will highlight this in the documentation for further users to be aware.

Best
Pavel

2 Likes

I spent 1 hour because of this :smiley: It would be great to have in documentation or in atlas data explorer as well.

Viren

1 Like

Hi @Lukas_deConantseszn1, I’m having a similar problem to you.

You mentioned you implemented logic to get it working, would you mind posting details? Could help me with my issue!

@Calum_Craig,

Welcome to MongoDB community.

I think @Lukas_deConantseszn1 had enabled a replace event because data explorer updates are actually replaces so it must catch those…

Thanks
Pavel