'How does firestore read write and delete calculated? [closed]

I am trying to analyze and optimize the number of Reads and Writes in my cloud Firestore Database.

  1. How Does Firestore calculate Reads and Writes?

  2. How much Reads does it cost when querying for a single document from a list of 10 documents from a collection?



Solution 1:[1]

Firestore calculates the reads and writes based on the number of documents returned.

Reads:

For example, If you read a document like this,

db.collection('users').doc(userId).get(); //it will cost you one read 

Let's assume your user collection has 100 documents.

db.collection('users').get(); //it will cost you 100 reads

When you query the collection, the cost is calculated based on the number of documents returned by the query.

For example

db.collection('users').limit(50).get(); //it will cost you 50 reads
db.collection('users').where('age','<','20').get(); //assume you have only 3 users are below age 20, this will only cost you 3 reads.

Writes:

Writes are calculated based on the number of documents create and update operations.

For example,

db.collection('users').add(data); //one write
db.collection('users').doc(userId).update(data); //one write

Batch Operations:

When a batch operation is made, that cost is calculated based on the number of documents that are affected by the batch.

batch.set(docRef1, data1); //one wrire
batch.set(docRef2, data2); //one write
batch.set(docRef3, data3); //one write
batch.delete(docRef3); //one delete

In total this batch will cost 3 writes & one delete.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1