3

I have multiple document. My problem is that I am not able to get the particular data which modified, I am getting full documentions.

 db.collection("employees").whereEqualTo("OID", OID)
                    .addSnapshotListener(new EventListener<QuerySnapshot>() {
                        @Override
                        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if (e != null) {
                    Log.w(TAG, "listen:error", e);
                    return;
                }

                for (DocumentChange dc : snapshots.getDocumentChanges()) {
                    switch (dc.getType()) {
                        case ADDED:
                             //Here i am getting full newly added documents
                            break;
                        case MODIFIED:
                           //here i am getting which is modified document,ok fine...,But i want to get only the filed which i modified instance of getting full documents..
                            break;
                        case REMOVED:

                            break;
                    }
                }

            }
        });

Note:

case MODIFIED: here i am getting which is modified of full document, which works fine. But I want to get only the field that I modified instead of getting all the fields from documents.

enter image description here

Take look of above screenshot

For example

If I change the field isActive=true into isActive=false then I want to get the particular filed only.,, I don't want the remaining all fields...(OR) How to identify which field is changed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54

1 Answers1

5

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:

And Todd Kerpelman's answer from:

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193