Skip to content

How to get count of related collection #1120

Answered by mpscholten
amitaibu asked this question in Q&A
Discussion options

You must be logged in to vote

One good way to solve this is by adding a comments_count : Int field to the posts table. Then whenever a new comment is created, update the comments_count of the related field.

Then you can use get #commentsCount post to access it from the view.

This also has the advantage that it keeps your application response times fast. Without having a comments_count field, the COUNT() queries would get slower over time as the comments in the database are growing. With the comments_count field the runtime is independent of the amount of comments that exist.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@amitaibu
Comment options

amitaibu Oct 9, 2021
Maintainer Author

@amitaibu
Comment options

amitaibu Oct 9, 2021
Maintainer Author

@mpscholten
Comment options

Answer selected by amitaibu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants