Skip to content
Back

Is there a way to get the count of a collection instead of retrieving all of the data?

  • 0
  • Databases
bugs_cena
16 Nov, 2023, 11:35

Something on the lines of

TypeScript
db = Databases(client)
db.count_documents(database_id, collection_id, queries=queries)
TL;DR
You can get the count of a collection without retrieving all the data using GraphQL's aggregation function. One workaround is storing the count in a separate metadata collection and updating it using functions tied to create and delete events.
frankenstein
16 Nov, 2023, 20:38

just query all documents then limit to 1 document. in the response you will get a response.total thats the total number of documents in that collection

frankenstein
16 Nov, 2023, 20:38

But this works on only documents less than 5000

Faisal7
22 Mar, 2024, 09:33

is it possible to do it unlimited?

Faisal7
22 Mar, 2024, 09:33

what if i have more then 5000 documents and want to get the total count?

frankenstein
22 Mar, 2024, 16:51

They put 5000 for faster querying. Maybe lets wait for experts, they might give a way to add the limit

Faisal7
22 Mar, 2024, 16:53

yah but right now i need this very urgent. One workarround would be to store the count of each collection seperately in another metadata collection, and increment or decreent it by a function, but how do i make sure that this count is actually in sync with the actual number of the ducuments.

pitcairn1987
11 Dec, 2024, 09:49

Hi, I have the same problem. I was thinking if it's not possible to do it better using GraphQL's aggregation funciton: https://docs.8base.com/backend/graphql-api/grouping-and-aggregations/

๛Shadowツ
15 Mar, 2026, 01:06

I tried this approach, and it's working well with me: ```ts const response = await tablesDB.listRows({ databaseId: APPWRITE_DATABASE_ID, tableId: collectionId, queries: [Query.limit(1), Query.orderDesc("$sequence")], });

return response.rows[0].$sequence; ```

๛Shadowツ
15 Mar, 2026, 01:10

It works well with more than 5000 rows and less bandwidth usage

frankenstein
17 Mar, 2026, 12:08

oooouh yeaaaaah, thats genius

frankenstein
17 Mar, 2026, 12:09

but what about count specific documents. by adding query. what happens to the sequence

๛Shadowツ
17 Mar, 2026, 12:13

sequence works like auto-incremental column, only the Issue with it when delete docs

๛Shadowツ
17 Mar, 2026, 12:13

in this case you use cursor to handle it

Ibaraki Douji
17 Mar, 2026, 21:39

the way appwrite team recommends to do it, is kinda to have a function tied to the create and delete rows events, and update a table with counts for all the others tables

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more