如何在流星中反应性聚合mongodb [英] How to reactively aggregate mongodb in meteor
问题描述
我是流星的新手.我已经建立了发布/订阅的概念.反应性地执行聚合时,我遇到以下错误.
I am newbie to meteor. I had established the publish / subscribe concept. I am facing the following error while performing aggregation reactively.
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
Template.header.helpers({
'tasks': function () {
console.log("tasks helper called : ");
Meteor.subscribe('reportTotals', function() {
console.log(clientReport.find().fetch());
});
return ['1', '2'];
},
});
服务器代码
import { Meteor } from 'meteor/meteor';
import {ReactiveAggregate} from 'meteor/jcbernack:reactive-aggregate';
Meteor.startup(() => {
console.log("Server Started");
// code to run on server at startup
var MONGO_URL = "mongodb://127.0.0.1:27017/test";
});
Meteor.publish("reportTotals", function() {
// Remember, ReactiveAggregate doesn't return anything
this.autorun(function () {
ReactiveAggregate(this, atm_data, [{
// assuming our Reports collection have the fields: hours, books
$group: {
'_id': null,
'bottles_used': {
// In this case, we're running summation.
$sum: '$BOTTLE_USED'
// $sum: 1
}
}
}, {
$project: {
// an id can be added here, but when omitted,
// it is created automatically on the fly for you
bottles_used: '$bottles_used'
} // Send the aggregation to the 'clientReport' collection available for client use
}], { clientCollection: "clientReport" });
});
});
刷新DDP缓冲写入时发生异常:错误:期望找到要更改的文档 在Object.update( http://localhost:3000/packages/mongo中. js?hash = ed0b13aca2f180af120dd0cfdba64ac79e2a624f:246:29 ) ... 预先感谢.
Exception in flushing DDP buffered writes: Error: Expected to find a document to change at Object.update (http://localhost:3000/packages/mongo.js?hash=ed0b13aca2f180af120dd0cfdba64ac79e2a624f:246:29) ... Thanks in advance.
推荐答案
您没有客户端集合.另外,您需要先订阅,然后才能调用此助手.
You don't have a client side collection. Also, you need to subscribe before you call this helper.
尝试
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
var clientReport = new Mongo.Collection('clientReport');
Meteor.subscribe("reportTotals");
Template.header.helpers({
'tasks': function () {
console.log("tasks helper called : ");
console.log(clientReport.find().fetch());
},
});
您也不需要管道并且不需要在服务器代码上自动运行,请尝试以下操作:
You also don't need the pipeline and no autorun on server code, try this:
AtmData = new Mongo.Collection('atmdata');
Meteor.startup(() => {
// code to run on server at startup
/* AtmData.insert({
bottles_used: 123,
}); */
});
Meteor.publish("reportTotals", function() {
// Remember, ReactiveAggregate doesn't return anything
ReactiveAggregate(this, AtmData, [{
// assuming our Reports collection have the fields: hours, books
$group: {
'_id': null,
'bottles_used': {
// In this case, we're running summation.
$sum: '$bottles_used'
// $sum: 1
}
}
}], { clientCollection: "clientReport" });
});
这篇关于如何在流星中反应性聚合mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!