I’m trying to figure what is the best way to achieve the following:
Let’s say I have a blog app with two collections:
Meteor.users - standard useraccounts collection
Posts - stores blog posts, has author field which stores user _id
Now I would like to show a list of authors with their post counts, so it would go like this:
Author A: 1 post
Author B: 3 posts
Author C: 0 posts
How can I achieve this? I was thinking about making a custom low-level publication (creating the cursor by myself), but that seems overly complex for something that can be achieved using one SQL query in any of the standard web languages/frameworks (Rails, PHP).
on the SQL note - you can do the same easily with aggregations, both are non-reactive
if you want to have reactive count, you have like 2 options
simple count for queries/cursors
manage separate collection which would have count numbers.
Performance depends on changes/reads ratio.
If you have cursor without arguments and all access it, it would be computed just once and served to all clients.
If you want some filters for example list of the authors you wanna count(or using this.userId inside cursor), than maybe separate collection should be considered etc… but all these fine tunings can be done later and you can switch quite easily, if you structure your app correctly.
Can you elaborate on what exactly non-reactive means in this context? I understand that it won’t be updated on client in real-time, but then if another post gets added will the publication function get recomputed so it will server current data upon next page refresh?
publish-counts does the trick quite well, but - as the documentation says - it is not designed for large result sets, as it looks at each and every search result object.
See also this thread:
Does anybody know a package that “just” uses the cursor count for counting, but in a reactive way?
For it to be reactive, on the server side it should observe when data gets added or removed then send that it has changed. However, that would likely involve many updates from what I can tell.
I am thinking that it is best to just do a service call to get the counts when needed (in my case it’s because I changed the search criteria), but it would not detect that there is a change on the server side immediately as this is not a publication.