I’ve been thinking about this for a few days and I think my head is going to explode… I hope you can help me by bringing some light to the issue.
I´m using Meteor 1.10 + Blaze
I have a collection with the data of the authors and another collection with the posts that these authors have published. Each author is the creator of many post.
I want to show a list where it appears the name of each author and the title/date of their last post, for example:
Schema examples:
author_schema = {
_id: 'JsQEJG3qsBrKE6hep',
name: 'Bryan',
surname: 'Lopez',
}
post_schema = {
_id: 'Ftxw9YKTaxnKtJztg',
tittle: 'how to be happy',
date: ISODate("2020-01-30T10:00:46.572Z"),
authorId: 'JsQEJG3qsBrKE6hep'
}
It is clear to me that I have to publish all the authors, but what I want to avoid is to publish all the posts to the client side, because it slows down the loading and is inefficient.
Is there any way to avoid publishing all the posts and be able to only publish the last one post (the one with the most recent date) of each author?
Thanks a lot for your help! Meteor rules!
Ok, that is pretty simple. The most efficient way is to save 2 extra fields on the author collection and upsert these fields every time a post is being created.
Again: when you save a post to the posts collection, save lastPostName and lastPostDate on the author collection and overwrite with every post. You can also add the lastPostId so that you can easily build a url to that particular post.
Example (all this data stored in the author collection):
<span>
<a target="_blank" href="....your url/dynamic...postId">{Your dynamic post name}</a>
<span>{Your dynamic post date}</span>
</span
If you want to do searches like: authors with a last post today or last 7 days that would come pretty easy and efficient. Just to make sure you have a index on your collection for any field you want to use for the search.