Limiting a trigger based on the update belonging to a specific field of a subdocument

I’m also looking for some guidance on limiting a trigger based on the update belonging to a specific field of a subdocument. In my case the Match Expression works if I’m matching on a specific value but fails with more complex operators.

Works:
{"updateDescription.updatedFields":{"participation.2021.0.status":"accepted"}}

Does Not Work:
{"updateDescription.updatedFields":{"participation.2021.0.status":{"$exists":true}}}

Also Does Not Work:
{"updateDescription.updatedFields":{"participation.2021.0.status":{"$eq":"accepted"}}}

It would be great to get some more detailed information on how Trigger Match Expressions are evaluated and what sort of limitations / workarounds there are.

Update with solution, thanks to some help from MongoDB Support.

Because of the vagaries of how the change stream is formatted, it’s non-trivial to get a trigger to fire on any change to a particular nested field. This general structure, however, worked for me in the trigger’s match expression:

{
        "$expr": {
            "$not": {
                "$cmp": [{
                    "$let": {
                        "vars": { "updated": { "$objectToArray": "$updateDescription.updatedFields" } },
                        "in": { "$arrayElemAt": [ "$updated.k", 0 ] }
                    }},
                    "example.subdocument.nested_field"
                ]
            }
        }
    }

(just replace example.subdocument.nested_field with the proper dot-notation path to your field)

1 Like

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