0

I've been using Google Cloud for a client's project. I've used App Engine + Firestore, and I had to implement pagination for a big table of logs.

I have to say, it's been really challenging and I'm still not happy with it. In my use case I need to:

  • Paginate without any filter
  • Paginate the results of a query

In the first case, I just need to have a counter store somewhere that is increased every time a new log is added to the collection. Weird but easy enough. In the second case, I need to have the number of items in the query's result, and I had to create another "aggregation" field that saves a counter for each of the values that the field can have. And I'd have to do this for each field!

I'm wondering if I'm missing something here, or if Firestore is just not good for my use case.

How did you approach pagination with Firestore?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Have a look at the documentation on this topic: https://firebase.google.com/docs/firestore/query-data/query-cursors – Renaud Tarnec Apr 17 '20 at 12:56
  • @RenaudTarnec thank you. I've read that page many times actually, but it doesn't have a solution for my problem. I'd like to know if there's an efficient way to get the total number of items of a subset of a collection (based on some filters), – thebluebird Apr 17 '20 at 13:32
  • 1
    There isn’t any out of the box aggregation capabilities in Firestore. You will need to maintain your own counters in the back end or calculate them in the front end. This answer about counting docs in a query may interest you: https://stackoverflow.com/questions/61250180/how-to-get-the-number-of-documents-under-a-firestore-collection/61250956#61250956 – Renaud Tarnec Apr 17 '20 at 13:40
  • @RenaudTarnec thank you. That's what I'm doing already and I realized it doesn't work well with paginating queries' results. After some research and talking with other people, I understand now that Firestore encourages the use of cursors over offset, and this mean that in the end it comes to an UX decision: showing the number of remaining items or have infinite scrolling? For the former, Firestore is definitely not a good choice, most of all if you want to paginate after a query – thebluebird Apr 18 '20 at 06:16
  • [Here](https://medium.com/firebase-tips-tricks/how-to-implement-pagination-in-firestore-using-jetpack-compose-76b4c0b5acd50) is a working solution. – Alex Mamo Jan 24 '22 at 13:50

0 Answers0