Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - Firestore - how many time will I read documents

With the new Firestore from Firebase, I discovered that I have poor knowledge with Observables.

My problem is the following:

I get some data with db.collection('room').

  1. If I don't listen to the observable with a subscription, do I fetch the document? (I think so).

  2. For every change in my collection "room", is it considered as a "new document read" by Firestore?

  3. If I have duplicated Observables which return db.collection('room') in my app, will I have X calls to the Firestore database or just one?

Thanks!

like image 934
Wandrille Avatar asked Oct 15 '17 10:10

Wandrille


People also ask

What counts as a read on Firestore?

Cloud Firestore allows you to listen to the results of a query and get realtime updates when the query results change. When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated.

How do I count documents in firestore?

If you need a count, just use the collection path and prefix it with counters . As this approach uses a single database and document, it is limited to the Firestore constraint of 1 Update per Second for each counter.

How many documents can firestore handle?

There is no limitation on the number of documents in Firestore collection but it has a limitation of the size of the document. The maximum size of a document is roughly 1 MiB (1,048,576 bytes). Show activity on this post.

Why are my firestore reads so high?

Use of the Firebase console will incur reads. If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads.


1 Answers

  1. If I don't listen to the observable with a subscription, do I fetch the document? (I think so).

When you call var ref = db.collection('room'), ref is not really an observable it is a reference to the 'room' collection. Creating this reference does not perform any data reads (from network or disk).

When you call ref.get() or ref.onSnapshot() then you are fetching the documents from the server.

  1. For every change in my collection "room", is it considered as a "new document read" by Firestore?

If you are listening to the whole collection (no where() or .orderBy() clauses) and you have an active onSnapshot() listener then yes, you will be charged for a document read operation each time a new document is added, changed, or deleted in the collection.

  1. If I have duplicated Observables which return db.collection('room') in my app, will I have X calls to the Firestore database or just one?

If you are listening to the same Cloud Firestore data in two places you will only make one call to the server and be charged for the read operations one time. There's no cost/performance penalty to attaching multiple listeners to one reference.

like image 109
Sam Stern Avatar answered Oct 19 '22 19:10

Sam Stern