I have a trigger that I would like to run when a specific array field is updated on a document:
My update query looks like this:
db.collection.update(
{ _id: itemId },
{
$addToSet: {
'userProfile.someArray': "string",
'userProfile.someArrayHistory': {
userId,
date: new Date(),
action: 'action_taken',
}
}
}
);
My trigger $match
expression looks like this:
{
"updateDescription.updatedFields.userProfile.someArray": { "$exists": true }
}
also tried this:
{
"updateDescription.updatedFields.userProfile.someArray": { "$gte": { "$size": 0 } }
}
When I run the $addToSet
update, the trigger does NOT run, however, when I do a $set
command like below, the trigger runs as expected:
db.collection.update(
{ _id: itemId },
{
$set: {
'userProfile.someArray': "string",
'userProfile.someArrayHistory': {
userId,
date: new Date(),
action: 'action_taken',
}
}
}
);
Why is this? Is this a feature or a bug?
Seems like anything outside simple updates to top level fields in the document and triggers fall apart. Nested fields and arrays have been extremely challenging.
Thanks.