0

In a case of having the following structure:

Collection of albums where each album has a collection of stickers and each sticker has a type.

What is the best approach to count the total number of stickers I have by type, independent of which album the sticker is? I will need to fetch all documents or firestore allows this kind of complex queries?

Felipe Porge Xavier
  • 2,236
  • 6
  • 19
  • 27
  • 1
    Firestore doesn't offer any aggregation type queries. If you want a count of documents without paying for a read of each document, you will have to maintain those counts yourself as the data changes over time. – Doug Stevenson Nov 24 '19 at 19:49
  • @DougStevenson I don't fully agree with this being a duplicate. The other question is about getting the count of a collection, which isn't exactly the same as getting the count of items based on a certain 'filter' (sticker type in this case) – dumazy Nov 24 '19 at 20:24
  • 1
    @dumazy It turns out that both situations are considered queries in Firestore. If you request all the documents in a collection, that is just a query without a filter. Adding a filter doesn't really change the capabilities of the query. In both cases, a count can only be obtained by executing the query and looking at the size of the result set. So, for both cases, if you want a count without incurring the cost of reading all the documents, there needs to be a running tally. The difference is a matter of knowing which combinations of filters need to be counted. – Doug Stevenson Nov 24 '19 at 20:33

0 Answers0