gpt4 book ai didi

mongodb - Meteor + MongoDB - 显示帖子列表及其评论数

转载 作者:可可西里 更新时间:2023-11-01 09:52:45 27 4
gpt4 key购买 nike

我是 Meteor 的新手,所以这个问题听起来可能很尴尬。我正在尝试显示所有帖子的列表

Posts = new Meteor.Collection('posts');

然后在 Meteor.publish('posts', ...)

return Posts.find();

并显示与每个帖子相关的评论数量。评论存储在一个单独的集合中

Comments = new Meteor.Collection('comments')

我不希望用户从数据库中下载所有评论只是为了找出每个帖子的评论数 - 我不会在这里显示它们。所以

Meteor.publish('comments', function(){
return Comments.find();
})

不是一个选项。

我知道我可以对数据进行反规范化并将 commentsCount 存储在 Post 文档中。但是还有其他方法可以做到这一点吗?我希望它是可观察的——或者更确切地说,当然是实时更新。我知道如何显示单个帖子,但我不知道如何显示整个列表。

最佳答案

您知道可以向发布函数添加参数吗?

你可以有类似的东西

Meteor.publish('comments', function(post){
return Comments.find({postId: post._id});
})

这样您一次只能获得一篇文章的评论。

希望这就是你要找的

关于mongodb - Meteor + MongoDB - 显示帖子列表及其评论数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com