Finally managed to get it working. Most examples left out the connect()
part.
My working code:
Book.getDataSource().connector.connect(function(err, db) {
var collection = db.collection('Book');
var author = Book.getDataSource().ObjectID(authorId);
collection.aggregate([
{ $match: { authorId: author } },
{ $group: {
_id: authorId,
total: { $sum: "$price" }
}}
], function(err, data) {
if (err) return callback(err);
return callback(null, data);
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…