Skip to content
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

Closed
narsariamanu opened this issue Aug 2, 2018 · 13 comments
Closed

Firestore - Count number of documents in a collection #5731

narsariamanu opened this issue Aug 2, 2018 · 13 comments
Labels
api: firestore Issues related to the Firestore API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@narsariamanu
Copy link

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?

@tseaver tseaver added type: question Request for information or clarification. Not an issue. api: firestore Issues related to the Firestore API. labels Aug 2, 2018
@tseaver
Copy link
Contributor

tseaver commented Aug 2, 2018

@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?

@narsariamanu
Copy link
Author

@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.

@tseaver tseaver added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed type: question Request for information or clarification. Not an issue. status: awaiting information labels Aug 3, 2018
@tseaver
Copy link
Contributor

tseaver commented Sep 6, 2018

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 tseaver closed this as completed Sep 6, 2018
@narsariamanu
Copy link
Author

@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.

@tseaver
Copy link
Contributor

tseaver commented Sep 6, 2018

@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.

@nodkrot
Copy link

nodkrot commented Sep 8, 2018

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?

@schmidt-sebastian
Copy link

For some documentation, please take a look at https://firebase.google.com/docs/firestore/query-data/query-cursors

@pierre-b
Copy link

A workaround is to:

  1. write a counter in a firebase doc, which you increment within a transaction everytime you create a new entry

  2. You store the count in a field of your new entry (i.e: position: 4).

  3. 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!

@imaksp
Copy link

imaksp commented Apr 4, 2020

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.

@PragadeeshwaranPasupathi

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);
})

@leandroz
Copy link

@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.

A acordar 2020-07-31 09-00-47

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.

@sreeram-dev
Copy link

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

@nthnca
Copy link

nthnca commented Feb 9, 2023

x = client.collection("collection").count().get()
x[0][0].value

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.

@tseaver tseaver removed their assignment Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

10 participants