Reputation: 958
I'm using pagination to reduce the number of calls with Firestore, I managed to set it up correctly.
But I also need to get the total number of all the documents upfront corresponding to the query which looks something like this:
let query = db.collection("Posts").document(uid).collection("Posts").order(by: "timestamp", descending: true)
If I need to get the total number of documents upfront what's the point in paginating? Is there a point to it?
To be clear I only need the number of documents and not to read them all at the beginning.
Upvotes: 0
Views: 41
Reputation: 3499
More to your point: you generally do NOT need to know the total # of documents to page through the documents. Just keep paging forwards until the query returns fewer documents than your specified page size - which means you've reached the end.
I have libraries that page both forward and backward through a query - in my case, sorted alphabetically - so it's obvious from the displayed data approximately where the paging is in the collection. I'm sure your data-sorted collection is similar. It's not the count users care about - it's approximately how close to the end they are.
This is a basic "feature" of NoSQL, and rather than fighting it and complaining, design your UI so that it isn't an issue.
Upvotes: 1
Reputation: 317352
Firestore does not make it possible to get a document count without executing a query. Any counts or other aggregations that you require will need to be computed as a running tally and stored elsewhere, perhaps in another document, requiring at least a single read of that document.
See also:
Upvotes: 1