I can find the implementation of counting the number of documents in a collection in all the libraries except the Python library.
Am I looking in the wrong place or this feature is still pending?
@narsariamanu I'm not sure what you mean -- the google-cloud-firestore Python library allows you to list the documents in a collection:
from google.cloud.firestore_v1beta1 import Client
client = Client()
collection = client.collection('my_collection')
documents = list(collection.get())
print("# of documents in collection: {}".format(len(documents)))
But the backend API doesn't allow querying just for the count. Can you point to the examples you found?
@tseaver The code snippet uploaded by you solves my problem but I could not find any reference to counting the number of documents in a collection in the google-cloud-firestore documentation found query documentation or collection documentation
In case of python this whole process needs to be done manually whereas in case of libraries for
Javascript, Android, NodeJs there are ready wrappers around it.
It would be great to have something similar in the python library. Also, please get this solution added to the library's documentation so that its easier to find by other developers as well.
There isn't any QuerySnapshot object in the google.cloud.firestore library. The Query object doesn't know how many documents it contains: you have to perform the get() API call, which returns an iterator over the set of documents. That iterator cannot know how many documents it contains, as they are fetched via a gRPC stream.
@jba, @schmidt-sebastian, please reopen if I'm mistaken about the feasibility here.
@tseaver that's how I was count now, but this way I have to read all the documents just to count them. If possible please treat this as a feature request.
@narsariamanu It isn't possible, as I explained above: the data is not available. The links you referenced in the firebase docs aren't relevant here: they are based around a client-side architecture where the set of documents has already been fetched.
Firestore is great, I was able to get my blog running in almost no time until I couldn鈥檛 get a basic pagination work without total collection count :(. Solution with google functions to increment/decrement is just making this great experience into very unpleasant one. Please consider this as feature request! +1
Any ideas how I can make a basic pagination meanwhile?
For some documentation, please take a look at https://firebase.google.com/docs/firestore/query-data/query-cursors
A workaround is to:
write a counter in a firebase doc, which you increment within a transaction everytime you create a new entry
You store the count in a field of your new entry (i.e: position: 4).
Then you create an index on that field (position DESC).
You can do a skip+limit with a query.Where("position", "<" x).OrderBy("position", DESC)
Hope this helps!
firestore should add support for count method like other databases, we can store spearate counter if Table in server paging has no filters but what if it has many conditional filter, we can't set different counter for each filter condition.
luckily , i've solved somehow using the code,
try this, and it works well .
this.db.collection('User').valueChanges()
.subscribe( result => {
console.log(result.length);
})
@PragadeeshwaranPasupathi doing that you are reading all the documents, the idea is to be able to count with cost O(1).
My use case is a bit more complex than just reading all the documents in a collection. I have different "views", each view has a query associated that let the users see a specific subset of a collection of "tickets". I need to show the number of tickets on each view.

What I am currently doing is keep a count on each view, that gets updated every time a ticket is created or updated, of course I am not running the whole query, but testing the query against that single document (transforming firestore query syntax to js) and incrementing or decrementing the count.
One problem with the approach, is that some queries are Date dependent, for example "With messages the last week", I need to recalculate them once a day so tickets with old messages are decremented.
I think the best possible solution is to have something server side from Firestore where you pass a query and returns just the count, and for those scenarios to compute as a single read.
I don't think the problem in that case is technology but the willing of the Firebase team to make some cost compromises and release that feature to their customers, no need to transfer the data over the wire, just a single number.
O(1) counts are definitely a requirement in firestore. Since firestore is gaining acceptance this is definitely a much needed feature for parity with other document stores
Most helpful comment
@tseaver that's how I was count now, but this way I have to read all the documents just to count them. If possible please treat this as a feature request.