Update fields only on other field change

I want to update two additional fields (counter and date) only if another field has changed.
A straightforward solution is to query this field and to update data only if it differs. I sure should be a more effective way to do it. Any help? :slight_smile:
Also, I want to support upsert, i.e. the document existence is not guaranteed before the update)

Hi @Orlovsky_Alexander,

If you need to update the fields based on another specific field or operation you better use a change stream logic:
https://docs.mongodb.com/manual/changeStreams/#modify-change-stream-output

You can set a pipeline to look for an existance of the field you watch in updateDescription change event field and perform the other 2 updates based on _id of the changed document.

For more details see the change event description:
https://docs.mongodb.com/manual/reference/change-events/

Since the logic triggered can be any MongoDB code, you can run an upsert and it will trigger the event.

Best
Pavel

1 Like

Thanks! It’s a clever solution. But looks not very practical. Because I need to start a change stream on every update/upsert call and guarantee concurrency safety to avoid the double counter updates. Documents are (only) updated concurrently by external API calls.

Hi @Orlovsky_Alexander,

Not sure what you mean by every update call… You can have a change stream running on the whole database and capturing events as they come in:
https://docs.mongodb.com/manual/reference/method/db.watch/#example

However collection level watch will also continually trigger in up coming updates. Moreover, you can resume the change stream so it is actually practical and used in many MongoDB components like Atlas triggers and mongodb connectors. Theoretical option is to use a kafka connector to sync collections but it sounds like an overkill to me.

Each event will trigger it once and with retrayble writes you can make sure that it is retried currectly.

Another way to do this is by calculating the needed values as part of every update statement if the update is done on the same collection, possibly by doing pipeline updates:
https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/

Best
Pavel