
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
- Network error when attempting to fetch r...
Hi, I am trying to modify some database data in the console for testing but keep getting this error. I am on the appwrite cloud and have already tried clearing ...
- Having errors migrating to cloud
Project will not migrate compeltely
- Appwrite realtime stopped working all of...
In our production environment, Appwrite Realtime suddenly stopped working and no updates are coming through , can you confirm if there are any known issues?
