iDecode
iDecode

Reputation: 28906

Does updating 2 fields at different path at the same time counts as 2 write in cloud firestore?

Screenshot 1:

enter image description here

At student > 100 > name, I want to update 'name' to 'Bar'.


Screenshot 2:

enter image description here

At student > 100 > exam > english > marks, I want to update 'marks' to 50.


Now, I am able to update them using

var firestore = Firestore.instance;
firestore.document('student/100').updateData({'name': 'Bar'});
firestore.document('student/100/exams/english').updateData({'marks': 50});

Problem:

This code gets called two times:

var ref = firestore.document('student/100');
ref.snapshots().listen((event) {
  print('server updated'); // called 2 times
});

Will this be counted as two write? If yes, how can I change it to one write because both the fields belong to same path 'student/100'?

Upvotes: 0

Views: 51

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Will this be counted as two write?

Yes, it will count as two write.

When you use Cloud Firestore, you are charged for the following:

  1. The number of reads, writes, and deletes that you perform.
  2. The amount of storage that your database uses, including overhead for metadata and indexes.
  3. The amount of network bandwidth that you use.

If yes, how can I change it to one write because both the fields belong to same path 'student/100'?

In Firestore, queries are shallow which means when you are writing to a document under a top level collection you cannot access the documents inside the subcollection unless you specificy the path toward that document. Therefore it is not possible to perform one write for both the top level document and the document inside subcollection.

Upvotes: 2

Related Questions