聚合计数查询在 Meteor 1.8 中不再有效

Aggregate count query is not working anymore in Meteor 1.8

我一直在查询中使用聚合来获取总记录数,但今天我从 Meteor 1.2 迁移到 1.8,现在以下查询出现异常:

我在服务器端使用 meteorhacks:aggregate @ 版本 1.3.0

查询:

Articles.aggregate([
      { $match: {userId: 'a767GGhrd743Ghe349sd'} },
      { $group: { _id: null, count: { $sum: 1 } } }               
])[0];

异常:

TypeError: Cannot read property \'count\' of undefined\n

在 Meteor 1.2 中,同样的查询为我提供了计数器,但是当我更新到 Meteor 1.8 时,现在它不再工作了。

您正在使用的包 (meteorhacks:aggregate) 大约从 2015 年起就被废弃了。使用新的 Mongo >= 3.0.0(从 Meteor 1.7 开始引入)这个包将无法使用由于 Mongo 驱动程序发生重大变化。

备选方案是针对 the latest Mongo driver or to use an active maintained fork of the package like sakulstra:aggregate.

自行实现聚合功能

分叉包本身 described the problem as well:

meteorhacks/meteor-aggregate seems pretty unmaintained, so let's maintain a fork! Meteor 1.7 and it's upgrade to mongodriver v3 introduced some breaking changes which will break meteorhacks:meteor-aggregate.