尝试使用monbro:mongodb-mapreduce-aggregation
运行超级简单的mapReduce函数。虽然它第一次运行很好,后续调用我得到以下错误:
I20150608-11:11:05.294(-4)? Exception while invoking method 'exportDonations' Error: A method named '/donation_totals_by_donor/insert' is already defined
I20150608-11:11:05.294(-4)? at packages/ddp/livedata_server.js:1461:1
I20150608-11:11:05.294(-4)? at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
I20150608-11:11:05.294(-4)? at [object Object]._.extend.methods (packages/ddp/livedata_server.js:1459:1)
I20150608-11:11:05.294(-4)? at [object Object].Mongo.Collection._defineMutationMethods (packages/mongo/collection.js:904:1)
I20150608-11:11:05.294(-4)? at new Mongo.Collection (packages/mongo/collection.js:209:1)
I20150608-11:11:05.295(-4)? at [object Object].ns.Collection (packages/matb33:collection-hooks/collection-hooks.js:190:1)
I20150608-11:11:05.295(-4)? at [object Object].Meteor.methods.exportDonations (app/server/index.js:44:19)
I20150608-11:11:05.295(-4)? at [object Object].methodMap.(anonymous function) (packages/meteorhacks:kadira/lib/hijack/wrap_session.js:160:1)
I20150608-11:11:05.295(-4)? at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
I20150608-11:11:05.295(-4)? at packages/ddp/livedata_server.js:648:1
这是我的Meteor方法:
var Reduced = new Mongo.Collection("donation_totals_by_donor");
var map = function() {
if(!this.donor) {
return;
}
emit(this.donor, this.amount);
}
var reduce = function(donorId, totalAmount) {
return Array.sum(totalAmount);
}
var result = Donations.mapReduce(
map,
reduce,
{
query: { createdAt: { $gte: new Date('Jan 1, ' + year), $lt: new Date('Jan 1, ' + (year + 1)) } },
out: "donation_totals_by_donor",
verbose: true
}
);
return Reduced.find({}).fetch();
答案 0 :(得分:1)
此错误是多次定义集合的结果。您需要将Message:
Error: Wait timed out after 270ms
Stacktrace:
Error: Wait timed out after 270ms
at Array.forEach (native)
at runMicrotasksCallback (node.js:337:7)
at process._tickCallback (node.js:355:11)
From: Task: < anonymous wait >
代码移到方法之外。
通常,您可以在中心位置定义收藏集,例如: new Mongo.Collection
。但是,由于此代码仅在服务器上运行,您可以将其保留在此文件的顶部,或将其放在lib/collections/mycollection.js
之类的位置。