meteor Aggregation in a Server Method


Example

Another way of doing aggregations is by using the Mongo.Collection#rawCollection()

This can only be run on the Server.

Here is an example you can use in Meteor 1.3 and higher:

Meteor.methods({
   'aggregateUsers'(someId) {
      const collection = MyCollection.rawCollection()
      const aggregate = Meteor.wrapAsync(collection.aggregate, collection)

      const match = { age: { $gte: 25 } }
      const group = { _id:'$age', totalUsers: { $sum: 1 } }

      const results = aggregate([
         { $match: match },
         { $group: group }
      ])

      return results
   }
})