New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Firestore - Count number of documents in a collection #5731
Comments
@narsariamanu I'm not sure what you mean -- the 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 In case of python this whole process needs to be done manually whereas in case of libraries for 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 @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 |
Firestore is great, I was able to get my blog running in almost no time until I couldn’t 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:
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, this.db.collection('User').valueChanges() |
@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 |
x = client.collection("collection").count().get() I was just trying to figure this out, couldn't find anything in the documentation, but a little introspection and the count call seems to exist. Sorry, that is some ugly code, and not sure if there is supposed to be a different way to call this... but it seems to work. |
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?
The text was updated successfully, but these errors were encountered: