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?
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(); //