Reputation: 520
I've installed the mongodb-aggregation package but I get "undefined" returned when peforming an aggregation within a meteor method. I assume that I'm missing something fundamental. How should the aggregation be performed? Any advice would be great.
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...
Upvotes: 0
Views: 899
Reputation: 195
Most importantly, note that db.ratings.aggregate
has not a thing to do with meteor or meteor-mongo-extensions . Mongo itself has a native db.<collection>.aggregate()
function. So thats why it works in the shell.
Now for Meteor. Meteor uses a custom mongo driver so it can set up all the nice reactive aspects in Meteor.Collection(), among other things. As such, some of the mongo functions have not yet been implemented.
And finally meteor-mongo-extensions, that is effectively a hack of a hack. I have yet to confirm it I believe the issue can be found in this Github issue. Try to run this on the server outside the meteor method, just to be sure.
If your issue is that the package is broken you can try one of the many packages on Atmosphere that manage aggregation. mongodb-server-aggregation looks promising.
Upvotes: 2