【问题标题】:Aggregation with meteorhacks:aggregate (why would I ever use $out)?使用流星黑客聚合:聚合(我为什么要使用 $out)?
【发布时间】:2015-09-22 04:01:02
【问题描述】:

这是对这个问题的重要修改,因为我已将出版物更改为一种方法并缩小了问题的范围。

我正在使用meteorhacks:aggregate 计算并发布用户选择的一系列公司的公司估值数据的平均值和中值。选择保存在 Valuations 集合中以供参考,汇总数据来自 Companies 集合。

下面的代码可以一次性使用(尽管它不是反应式的)。但是,用户将针对数千个valuationIds 重新运行此聚合。由于$out在插入新结果之前会先清除新集合,所以这里不能使用,需要保留每个实例的结果。我不明白为什么会使用$out

有没有什么方法可以用聚合结果更新现有的 Valuation 文档,然后订阅该文档?

服务器/方法

Meteor.methods({
    valuationAggregate: function(valuationId, valuationSelections) {
        //Aggregate and publish average of company valuation data for a user-selected series of companies./
        //Selections are saved in Valuations collection for reference and data for aggregation comes from Companies collection.//
        check(valuationId, String);
        check(valuationSelections, Array);
        var pipelineSelections = [
            //Match documents in Companies collection where the 'ticker' value was selected by the user.//
            {$match: {ticker: {$in: valuationSelections}}},
            {
                $group: {
                    _id: null,
                    avgEvRevenueLtm: {$avg: {$divide: ["$capTable.enterpriseValue", "$financial.ltm.revenue"]}},
                    avgEvRevenueFy1: {$avg: {$divide: ["$capTable.enterpriseValue", "$financial.fy1.revenue"]}},
                    avgEvRevenueFy2: {$avg: {$divide: ["$capTable.enterpriseValue", "$financial.fy2.revenue"]}},
                    avgEvEbitdaLtm: {$avg: {$divide: ["$capTable.enterpriseValue", "$financial.ltm.ebitda"]}},
                    //more//
                }
            },
            {
                $project: {
                    _id: 0,
                    valuationId: {$literal: valuationId},
                    avgEvRevenueLtm: 1,
                    avgEvRevenueFy1: 1,
                    avgEvRevenueFy2: 1,
                    avgEvEbitdaLtm: 1,
                    //more//
                }
            }
        ];

        var results = Companies.aggregate(pipelineSelections);
        console.log(results);
        }
});

就在服务器上查看结果而言,上面的代码有效。在我的终端中,我看到:

I20150926-23:50:27.766(-4)? [ { avgEvRevenueLtm: 3.988137239679733,
I20150926-23:50:27.767(-4)?     avgEvRevenueFy1: 3.8159564713187155,
I20150926-23:50:27.768(-4)?     avgEvRevenueFy2: 3.50111769838031,
I20150926-23:50:27.768(-4)?     avgEvEbitdaLtm: 11.176476895728268,
//more//
I20150926-23:50:27.772(-4)?     valuationId: 'Qg4EwpfJ5uPXyxe62' } ]

【问题讨论】:

    标签: mongodb meteor methods aggregation-framework meteor-publications


    【解决方案1】:

    我能够通过以下方式解决此问题。需要添加forEach 以与$out 相同的方式展开数组。

    库/集合

    ValuationResults = new Mongo.Collection('valuationResults');
    

    服务器/方法

        var results = Companies.aggregate(pipelineSelections);
        results.forEach(function(valuationResults) {
            ValuationResults.update({'result.valuationId': valuationId}, {result:valuationResults}, {upsert: true});
        });
        console.log(ValuationResults.find({'result.valuationId': valuationId}).fetch());
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多