Created
May 16, 2014 03:56
-
-
Save nnashwin/8f1d5b394c30edc7b4cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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