• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

和meteorhacks:aggregate聚合(为什么我会使用$ out)?

用户头像
it1352
帮助1

问题说明

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

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

I am using meteorhacks:aggregate to calculate and publish the average and median of company valuation data for a user-selected series of companies. The selections are saved in a Valuations collection for reference and the data for aggregation comes from the Companies collection.

下面的代码可以很好地使用一次(尽管它不是被动的).但是,用户将重新运行此聚合达数千个valuationId.由于$out在插入新结果之前会先清除新集合,因此我不能在这里使用它,我需要保留每个实例的结果.我不明白为什么要使用$out.

The code below works fine for one-time use (although it's not reactive). However, users will rerun this aggregation for thousands of valuationIds. Since $out will first clear out the new collection before inserting the new results, I can't use that here, I need to retain the results of each instance. I don't understand why $out would ever be used.

有什么方法可以添加聚合结果来更新现有的评估文档,然后订阅该文档?

Is there any way to just add update the existing Valuation document with the aggregation results and then subscribe to that document?

服务器/方法

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);
        }
});

只要在服务器上查看结果,上述代码即可正常工作.在我的终端中,我看到:

The code above works, as far as viewing the results on the server. In my terminal, I see:

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' } ]

正确答案

#1

我能够使用以下方法解决此问题.需要添加forEach来以与$out相同的方式展开数组.

I was able to resolve this with the following. Needed to add the forEach to unwind the array in the same way as $out.

lib/收藏集

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());
}
});

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfigik
系列文章
更多 icon
同类精品
更多 icon
继续加载