使用meteor-mongo-extensions执行聚合

时间:2013-10-14 22:25:09

标签: mongodb meteor

我已经安装了mongodb-aggregation软件包,但是当在meteor方法中执行聚合时,我得到了“undefined”。我认为我缺少一些基本的东西。如何进行聚合?任何建议都会很棒。

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...

1 个答案:

答案 0 :(得分:2)

最重要的是,请注意db.ratings.aggregate与meteor或meteor-mongo-extensions没有任何关系。 Mongo 本身具有原生db.<collection>.aggregate()功能。这就是为什么它在shell中工作。

现在为流星。 Meteor使用自定义mongo驱动程序,因此它可以在Meteor.Collection()中设置所有好的反应方面。因此,一些mongo功能尚未实现。

最后 meteor-mongo-extensions ,这实际上是黑客入侵。我还没有确认,我相信这个问题可以在Github issue找到。尝试在meteor方法之外的服务器上运行它,只是为了确定。

如果您的问题是包破坏了,您可以尝试在Atmosphere上管理聚合的众多包中的一个。 mongodb-server-aggregation看起来很有希望。