Realm Trigger when a field in document has a specific value

For my USERS collection, I want to trigger a function when a document is inserted or updated, but only if the “status” field of the inserted/updated document is not equal to an empty string.

Reading the documentation, I believe with the $match expression, I can look to see if the value of “status” is CHANGED to an empty string using the expression below:

{
  "updateDescription.updatedFields": {
    "status": ""
  }
}

But that is not what I want. I want to fire the trigger only if the value for “status” field AFTER update or insert is NOT an empty string. It could have been an empty string before update as well.

The solution so far for me is to let the trigger get fired, but then in the function I check for the value of the “status” field to do what I need to do, but that means the trigger will be fired too many unnecessary times since the “status” field has an empty string 80% of the time.

Hi @Akbar_Bakhshi,

You can use the $ne operator:

{
  "updateDescription.updatedFields.status": {
    "$ne": ""
  }
}

Thanks
Pavel

Hi @Pavel_Duchovny,
Thanks for the response. However, I think with this expression, the trigger will still be fired if the “status” value was originally an empty string and after the document is updated, it’s still an empty string (the update happened on other fields). Right?

What I am trying to accomplish is to check the “status” value AFTER THE UPDATE and no matter what the previous value was, if the new value is equal to an empty string, I do not want to fire the trigger. Is that possible?

Edit - I think Pavel’s answer is correct here, I made the mistake of thinking this post was the same question asked here


Hi Akbar - I don’t believe this is doable today. Do you mind adding it here for our Triggers feedback - Realm: Top (0 ideas) – MongoDB Feedback Engine

This will allow us to track interest around more Triggers flexibility in the way you described

1 Like

I see.

Ok I just did: Firing a trigger if the added or updated document has a field with specific value.

Thank you!

Hi @Akbar_Bakhshi,

Have you tried in that case filtering on the main fullDocument (set update Lookup to true):

{
'fullDocument.status' : { "$ne" : "" }
}

Thanks
Pavel

1 Like

Hi @Pavel_Duchovny,
I think that’s it. It works now the way I want it to. I did not know that we have access to the fullDocument in the Realm UI. That’s great!

Thanks for the help!

Seems like this is already accounted for. We do have access to fullDocument in the Realm UI and so we can do the check in the advanced section using what Pavel suggested.

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