Skip to content

Instantly share code, notes, and snippets.

@nnashwin
Created May 16, 2014 03:56
Show Gist options
  • Save nnashwin/8f1d5b394c30edc7b4cf to your computer and use it in GitHub Desktop.
Save nnashwin/8f1d5b394c30edc7b4cf to your computer and use it in GitHub Desktop.
if (Meteor.isServer) {
var path = __meteor_bootstrap__.require('path');
var MongoDB = __meteor_bootstrap__.require('mongodb');
var Future = __meteor_bootstrap__.require(path.join('fibers', 'future'));
var Books = new Meteor.Collection('Books');
Meteor.startup(function () {
Books.aggregate = function(pipeline) {
var self = this;
var future = new Future;
self.find()._mongo.db.createCollection(self._name, function (err, collection) {
if (err) {
future.throw(err);
return;
}
collection.aggregate(pipeline, function(err, result) {
if (err) {
future.throw(err);
return;
}
future.ret([true, result])
});
});
var result = future.wait();
if (!result[0]) {
throw result[1];
}
return result[1];
};
});
Meteor.methods({
myAggregationMethod: function() {
return Books.aggregate([ { $group: { _id: null, total: { $sum: "$counter"} } } ] );
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment