【发布时间】:2017-06-24 05:29:07
【问题描述】:
我正在尝试获取未知大小的数据集合的平均值,因此我想在服务器端编写类似这样的内容:
@Mileage = new Meteor.Collection("mileage")
Meteor.publish "average", ->
Mileage.group
initial:
count: 0
total: 0
reduce: (doc, out) ->
out.count++
out.total += doc.mileage
finalize: (out) ->
out.avg = out.total / out.count
我可以做一个meteor mongo,这段代码(当然翻译成Javascript)工作正常。如何访问 Collection.group 之类的聚合函数?或者有没有更好的方法来做到这一点,我错过了?
另外,是这样的反应。假设我在客户端上有订阅这个的东西,并且它在模板中被引用。当mileage 文档集合中的某些内容发生变化时,平均值也会发生变化。我能否依靠 Meteor 的响应式更新将其推送给客户端?
【问题讨论】: