firebase - Pass changed fields to cloud function when Firestore document is updated instead of passing old and new Firestore doc

admin2025-04-24  2

When I update a Firestore document, a Google Cloud Function is triggered with both the old and new documents. That is a lot of data passed from Firestore to the triggered Cloud Function.

Is there a way for the Cloud Function to only receive the changed fields of the Firestore document instead of receiving the old and new documents?

When I update a Firestore document, a Google Cloud Function is triggered with both the old and new documents. That is a lot of data passed from Firestore to the triggered Cloud Function.

Is there a way for the Cloud Function to only receive the changed fields of the Firestore document instead of receiving the old and new documents?

Share Improve this question edited Feb 14 at 8:42 Alex Mamo 139k18 gold badges169 silver badges201 bronze badges asked Jan 17 at 5:47 Aimn BlbolAimn Blbol 1,3051 gold badge16 silver badges33 bronze badges 3
  • Are you looking for this Stackoverflow Link? – Sandeep Vokkareni Commented Jan 17 at 8:17
  • No that is not what I am looking for. I don't want Firestore to trigger a cloud function on a certain field. What I want is for Firestore to tell me the exact fields that changed or updated. Right now, Firestore gives me the new and old documents. I only want the changed fields and not the full doc. – Aimn Blbol Commented Jan 17 at 8:46
  • This sounds like a XY problem. What is the actual problem here? – Frank van Puffelen Commented Jan 17 at 16:49
Add a comment  | 

2 Answers 2

Reset to default 1

No, it's not possible to change the way that the triggers work.

That is a lot of data passed from Firestore to the triggered cloud function.

If you're concerned about any costs associated with this data, don't be. You aren't paying for the data coming in to a function like this. Only HTTP type functions incur a variable cost for data generated by functions.

It is also not a very big overhead in terms of latency.

When I update a Firestore document, a Google Cloud Function is triggered with both the old and new documents.

That's the expected behavior, that unfortunately cannot be changed.

That is a lot of data passed from Firestore to the triggered cloud function.

As also @DougStevenson mentioned in his answer, you should not worry about the costs because the data that is coming into a function is free of charge.

Is there a way for the cloud function to only receive the changed fields of the Firestore document instead of receiving the old and new documents?

No, Cloud Firestore always returns complete documents. There is no way you can get only a slice of the document or a specific subset of the fields. It's the entire document or nothing.

Since there are currently no triggers for specific fields, only triggers on any change of a document, if you want to know which particular fields have changed, then I recommend you examine the changed document against the old one to figure it out. So assuming you have a collection of users, the code might look like this:

exports.updateUser = functions.firestore.document('users/{uid}').onUpdate((change, ctx) => {
  const beforeDoc = change.before.data(); //
转载请注明原文地址:http://anycun.com/QandA/1745489973a90783.html