1

I use getDocData function for get one document.

 Future getDocData(String docPath) async {
    try {
      print(docPath);
      final response = await fireStore.doc(docPath).get();
      return response.data();
    } catch (e) {
      throw UnimplementedError(e.toString());
    }
  }

After i got document i updated same document with updateDocument function.

 Future updateDocument<T>(docPath, docField, data) async {
    try {
      return await fireStore.doc(docPath).set(
        {docField: data},
        SetOptions(
          merge: true,
        ),
      );
    } catch (e) {
      throw UnimplementedError(e.toString());
    }
  }

when operation done. i expect on firebase usage dasboard one read and one write operation happen but I see two read and one write operation.Is there anyway solve this issue or is it okay?

Özgür
  • 21
  • 4
  • 1
    Have a look at this [article](https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e) & stackoveflow [link](https://stackoverflow.com/questions/50887442/cloud-firestore-how-is-read-calculated) – Sathi Aiswarya Mar 30 '23 at 13:23
  • The article is answered my question.Thanks for your reply. – Özgür Mar 30 '23 at 23:33

1 Answers1

1

I have tried updating the document with set and update and observed the reads and writes like below :

With set() I got two reads and one write in the dashboard. set() with merge instead of updating existing documents will create a new document copying existing fields while retaining the same doc id. That is where I got one extra read operation.

With update() it is updating the existing document and will get one read and one write operation.

The docfield passed as value should be a key, which means it needs to be passed with [] brackets. Take a look at the sample code.

Future updateDocument<T>(docPath, docField, data) async {
    try {
      return await fireStore.doc(docPath).update(
        {[docField]: data}, // ⇐ notice
      );
    } catch (e) {
      throw UnimplementedError(e.toString());
    }
  }
Sathi Aiswarya
  • 2,068
  • 2
  • 11
  • If i am not wrong.When i use update() method Firebase doesnt count read just one write but set() count one read also if you use ...update({field: FieldValue.increment(value)}) operation count one read and one write. – Özgür Apr 01 '23 at 01:41