Hello, please what's the best way to count document for a Particular user,
USE CASE A user created a post, and i would want to count the number of posts that user has made since he joined the platform but here is my code, i'm not too confident on it, and i would love your ideal about what to do
My Controller
userPostCounter({required uid}) async {
final document = await _postApi.userPostCounter(uid: uid);
final data = document.map((post) => PostModel.fromMap(post.data)).toList();
var counter = 0;
if (data.isNotEmpty) {
counter = data.length;
}
return counter;
}```
**My PostApi**
``` @override
Future<List<Document>> userPostCounter({required String uid}) async {
DocumentList doc;
doc = await _db.listDocuments(
databaseId: Common.dataBasesId,
collectionId: Common.postCollection,
queries: [
Query.equal('quickPosting', true),
Query.equal('uid', uid),
Query.isNull('repliedTo'),
],
);
return doc.documents;
}```
*Please, i'm i on the right track on this?*
You should maintain a count that increments and decrements because counting every time doesn't scale. It will get slower the more data they have
Pls, can you go deeper in your explanation.
Thank you
You could have a document for the user which contains a number with the amount of posts the user has. Whenever a post is created an Appwrite function could trigger that increases the count for the user. This way you don't have to query the whole posts collection for the user to know how many posts there are
Thanks
But I think I have an issue here, Is there a way I can accredit the already post count to the new document for each user that already have post?
Write a script to count it and store the count. Just make sure you paginate
okay... can i use function to loop through users then accredit the count to those user?
would that work?
Sure
thank you
can i also do this for Likes (liking of Post)?
because, what i have before is that i create an array attribute for a document, so when user likes the post, i added it to the post array document
Recommended threads
- I'm getting error Invalid `url` param: I...
``` 2025-10-26T12:52:02.292Z [error] AppwriteException: Invalid `url` param: Invalid URI. Register your new client (vercel.com) as a new Web platform on your pr...
- 🚨 Access Restricted Despite Active Cred...
Hello Appwrite Support Team, My organization “Dreamo World Entertainment” currently shows an “Access Restricted” warning due to unpaid invoices. However, my bi...
- Deploy tanstack-start on site
- I am trying to deploy my project build with tanstack-start on site. - issue i am facing is appwrite doesnt provide start command option so i can run 'npm run ...