我已经安装了 mongodb-aggregation 包,但是在流星方法中执行聚合时返回“未定义”。我认为我缺少一些基本的东西。应该如何进行聚合?任何建议都会很棒。
rate: function(ratingProp){
var user = Meteor.user();
var postId = ratingProp.postId;
var post = Posts.findOne({_id: postId});
var rateVal = ratingProp.rateVal;
// ensure the user is logged in
if (!user) {
throw new Meteor.Error(401, "You need to signin to rate.");
}
// ensure rating has rateVal
if (!rateVal){
throw new Meteor.Error(422, "No rating provided.");
}
// ensure rating has a post
if (!post){
throw new Meteor.Error(422, "Rating not associated with a post.");
}
Ratings.upsert({userId: user._id, postId: postId},
{$set: { rateVal: rateVal }}
);
// perform aggregation
var avgRate = Ratings.aggregate([
{$match:
// hard coded for testing
{postId: "D7f3WoDEGW3SqGKW9"}
},
{$group:
{
_id: null,
"avgRating":{$avg: "$rateVal"}
}
}
]);
// additional code...