How to identify which field is changed?
You cannot achieve this in Cloud Firestore. The new Cloud Firestore database has different concepts than Firebase real-time database and should not be confused. There are no field-level permissions or access to a document. It's the entire document, or nothing.
So if you want to change the field isActive=true
into isActive=false
, then you will get the entire document once it was modified and not only the property that you have changed.
Cloud Firestore listeners fire on the document level. There is no way to get triggered with just particular fields in a document. So the Firestore client-side SDKs always returns complete documents. Unfortunately, there is no way to request only a part of the document with the client-side SDK, although this option does exist in the server-side SDK's select() method.
If you want to get notified only of specific fields, consider adding an extra collection with documents that only contain those fields. So create that additional collection where each document just contains the data you need. This has also a benefit because will also allow you to more easily secure access to the different types of data. For that please see Gil Gilbert's answer from:
- How to get a list of keys in Firestore?
And Todd Kerpelman's answer from:
- How to allow only particular fields of a firestore document to be accessed publicly
This sort of data duplication is quite common in NoSQL solutions such as Firestore and for that, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding. It is for Firebase real-time database but same principles aplly to Cloud Firestore.