2

As the title suggests I would like to know how to get the total elements count of a paginated and filtered collection.

I have seen that many recommend, for the counting of the documents of the collection, to create a statistics document with the counter of the documents in the collection.

But if I need to implement a paged and filtered retrieval, how can I have the count of the total filtered items without having to retrieve them all?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Simone
  • 33
  • 4
  • Count how many filtered documents you have in the collection for every filter you want to apply, and save it in one document about the whole collection. – Mises Oct 03 '22 at 08:29
  • I should do this for every combination of filters I have on the form. What if a filter involves the insertion of free text? It is not possible to do such a thing. – Simone Oct 03 '22 at 09:53
  • In Firebase, you can only search/filter by fields, not a text inside fields. – Mises Oct 03 '22 at 10:14

1 Answers1

2

Edit: October 20th, 2022

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() 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.


I have seen that many recommend, for the counting of the documents of the collection, creating a statistics document with the counter of the documents in the collection.

Yes, that is correct. It's very costly to count the number of documents within a collection, each time you need that total number. So it's best to have a field in a document that contains that number and increment it each time a new document is added and decrement it each time a document is deleted.

But if I need to implement a paged and filtered retrieval, how can I have the count of the total filtered items without having to retrieve them all?

There is no way you can know ahead of time, how many documents exist in a collection without reading them all or reading a document that contains that information, as explained above.

The pagination in NoSQL databases is a little different than in SQL databases. In all modern applications, we paginate the data using an infinite scroll. If you understand Java, then you can take a look at my answer in the following post:

Here is also the official documentation regarding Firestore pagination that can be achieved using query cursors:

If you understand Kotlin, I also recommend you check the following resource:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Knowing this doesn't make me happy. This means that if I use firestore I cannot provide the user with information on the maximum number of items retrieved. It seems to me an incredible thing that cannot be done. Anyway thanks a lot for the answer Alex – Simone Oct 03 '22 at 09:48
  • You can provide the user the information, only if you store the number of documents even from the beginning. Btw, nice question ;) – Alex Mamo Oct 03 '22 at 09:57
  • 1
    @Simone Like I mentioned, if you group documents by some category or tag. Beside of total number of documents in the collection, you can count how many documents you have in a certain category or contain a certain tag. – Mises Oct 03 '22 at 10:22
  • Ok thanks for the tip Mises. I'll try to do as you suggest. – Simone Oct 03 '22 at 10:27