与meteorhacks:aggregate聚合(为什么我会使用$ out)? [英] Aggregation with meteorhacks:aggregate (why would I ever use $out)?
问题描述
这是对该问题的重要修改,因为我将出版物更改为一种方法并缩小了问题的范围.
我正在使用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 valuationId
s. 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' } ]
推荐答案
我能够使用以下方法解决此问题.需要添加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());
}
});
这篇关于与meteorhacks:aggregate聚合(为什么我会使用$ out)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!