我有一个评论集合和一个Page集合。评论属于页面。用户可以提出评论,并且我想要显示属于页面的评论的所有投票的总计总和。什么是这样做的好方法?我应该使用AutoValue来存储集合的集合值吗?
我正在考虑将总和保留为页面集合内的AutoValue。有没有办法偶尔触发重新计算AutoValue?我不需要实时更新总和,每5分钟一次就足够了。
或者这是一个坏主意?在模板中使用ReactiveVar进行计算还是其他更好?
编辑:真的,没有什么特别的设置。只需一个带有数字“投票”属性的评论集合和一个带有数字自动值“分数”的网页集合,该集合应计算投票数。 的网页:
Collections.Pages = new Mongo.Collection("pages");
var PageSchema = new SimpleSchema({
name: {
type: String,
min: 1
},
score: {
type: Number,
autoValue: function (doc) {
var maxValue = 1;
Collections.Comments.find({ pageId: doc.pageId }).map(function(mapDoc){
maxValue += mapDoc.votes;
});
return maxValue;
}
},
的评论:
Collections.Comments = new Mongo.Collection("comments");
var CommentSchema = new SimpleSchema({
pageId: {
type: String
},
name: {
type: String,
optional: true
},
votes: {
type: Number,
label: 'Total Votes',
defaultValue: 0
},
你可以发布模式和发布者的代码吗? – zim
请参阅编辑。发布者没有什么值得发布的内容,因为它现在只是简单地返回一篇文章及其评论。 – Cos