
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
- Queries Length Error.
Hi All, I am having a issues across my app with how i am getting data, and just wanted to check if this was an issues with the new changes on appwrite over the...
- Realtime is not working!!
It shows `Session is not valid`. I am using SSR login. If if set table permission `User(Read)` then realtime can not show update. But if I set table permission...
- The current user is not authorized to pe...
I'm just getting this error while it was working couple minutes ago, my users have permissions to access and create tables data but on some databases I'm just g...
