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 use the `count_documents` method to get the count of a collection instead of retrieving all of the data. This method works for collections with less than 5000 documents. Just query all documents and limit the response to 1 document. In the response, you will find a property called `response.total` which provides the total number of documents in that collection. Here's an example code snippet: ``` db = Databases(client) db.count_documents(database_id, collection_id, queries=queries) ```
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

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