How do I obtain the sum of a Loopback PersistedModel?
There does not seem to be a documentation on how to achieve that.
If possible I would like to avoid ha
Aggregate query with loopback
Products.getDataSource().connector.connect(function(err, db) {
var collection = db.collection('Products');
collection.aggregate(
[{ $match: { "productCode" : "WIN10-NoOS" } }]
).toArray(function(err,servicesData){
if(err){
}else{
cb(null,servicesData);
}
});
});
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);
});
});