MongoDB aggregation on Loopback

≡放荡痞女 提交于 2019-11-29 02:27:59

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);
  });
});

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);
           }

         });
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!