Tim Estes
Tim Estes

Reputation: 385

How many reads in Firestore am I performing?

Say I have a collection called text_messages in firestore that has 500 documents in it, and I know the doc_id of one of those documents.

I want to execute the following code to fetch the data of that document down:

doc = db.collection("text_messages").document("my_doc_id").get()
data = doc.to_dict()

So my question is this: does this count as 1 read or 500 reads? I'm asking from a billing standpoint and want to optimize cost.

Upvotes: 2

Views: 2859

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

Since the accepted answer can be easily misunderstood, I am posting an alternative.

Firestore charges for each document that is read for you on the server.

Since your example reads only a single document, you will be charged for one document read no matter how many documents are in the collection that you read it from.

Performing a query does not read every document that may match the conditions, but uses indexes for determining which documents match the conditions and then reads only those documents.

So in a Firestore query you get (at most) charged for the number of documents that the query returns, not for the number of documents that the database has to consider.

Upvotes: 9

Pushkin
Pushkin

Reputation: 3604

Firebase counts as one read for each query, it doesn't matter if your collection has 500 docs or 5000 doc. A single query counts as one read, even if it had to go through 500 docs searching for it in the cloud firestore.

Also keep in mind that, while listening for a query, you are charged for a read each time a document in the result set is added or updated.

// Reading a specific document,
db.collection("text_messages").document("my_doc_id")
    .get() // One read

// Using where query
db.collection("text_messages").where("sender", "==", "jack sparrow")
    .get() // One read

// Reading a whole collection
db.collection("text_messages")
    .get() // One read

You can read more here

Upvotes: 2

Related Questions