AtomicallyBeyond
AtomicallyBeyond

Reputation: 458

Firestore get document download count?

Is there a way to determine a read count for each document in Firestore? I would like to limit read counts to 100,000 per document.

Upvotes: 1

Views: 181

Answers (3)

Alex Mamo
Alex Mamo

Reputation: 139019

(2022-10-20) Edit:

Starting from now, counting the documents in a collection or the documents that are returned by a query is actually possible without the need for keeping a counter. So you can count the documents using the new [count()][1] method which:

Returns a query that counts the documents in the result set of this query.

This new feature was announced at this year's Firebase summit. Keep in mind that this feature doesn't read the actual documents. So according to the [official documentation][2]:

For aggregation queries such as count(), you are charged one document read for each batch of up to 1000 index entries matched by the query. For aggregation queries that match 0 index entries, there is a minimum charge of one document read.

For example, count() operations that match between 0 and 1000 index entries are billed for one document read. For A count() operation that matches 1500 index entries, you are billed 2 document reads.


Is there a way to determine a read count for each document in Firestore?

As also @FrankvanPuffelen mentioned in his answer, there is no API for doing that. If you need such a mechanism you need to create it yourself. That means that each time a user reads a document, you should increment a counter. That's pretty simple to implement since Firestore provides a really straightforward solution for that. To keep a counter for each read, you can increment a field in a document using ServerValue.increment(1).

Here are the docs for Android:

Here are the docs for iOS:

And here are for the web:

Upvotes: 2

DIGI Byte
DIGI Byte

Reputation: 4163

You could do this through cloud functions with onRequest or onCall:

  • Read a value from Realtime database
  • If the value is larger than 0, return the respective document.
  • Then decrement the value in Realtime database

Sources:

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599776

There is nothing built into Firestore to limit the number of reads for a specific document. There is a quota system (which a.o. is used to enforce the quota on the free plan), but that doesn't apply per document.

Upvotes: 1

Related Questions