I don't see anywhere in the Appwrite documentation query how to perform a count (*) for a collection based on filters. Is it possible or I need to take care of the counts manually when adding or removing a document?
In short, as of 0.15.X, there is no public API to get an exact count greater than 5000 documents with the Client API. This is because performing a count requires iterating over every database object and can be an expensive database operation.
One way to achieve what you want is to iterate over all of the data when the counts are needed using cursor based pagination.
import { Databases } from "appwrite";
const databases = new Databases(client, "[DATABASE_ID]"); // 'client' comes from setup
let count = 0
const queries = [];
const limit = 100;
const offset = undefined;
let cursor = undefined;
while (true) {
const page = await databases.listDocuments('movies', queries, limit, offset, cursor);
const length = results.documents.length;
if (length === 0) break;
count += length;
cursor = results.documents[results.documents.length - 1].$id;
}
console.log(count);
For more info, refer to the Pagination docs. As you can imagine, this can be slow and inefficient.
A better approach is to maintain a metadata collection that holds all of the counts. You can have Appwrite Functions to increment the count on document creation. When a client needs the info, it can fetch the data from the metadata collection.