Find number of entries in firebase document

I am trying to do pagination with a Firestore database. Is there a way for me to find the number of documents in my Firestore from my website using Javascript so that I can code my (prev) and (next) buttons for pagination accordingly?

>Solution :

When it comes to Firestore, you can only request pages of data of a particular size using the limit() function. That being said, you can start from the beginning of a query, and get the following pages of the same size. This operation should continue with other similar operations until there are no more documents left.

So you have to always start from page one, then go forward through the pages using the query cursors, by specifying which document was the last one in the previous query.

Unfortunately, there is no way you can jump between pages. You always have to read all the prior pages. Besides that, since the collections in Firestore don’t maintain a document count, you won’t be able to know how many pages of data there are ahead of time unless you create and maintain your own count of documents.

If you want to implement a modern way for handling pagination, you should consider implementing that so-called "infinite scroll". Facebook does that, Instagram does that. There are many examples out there.

I have also written an article called:

Where I have explained how you can count documents in Firestore.

Leave a Reply