-2

I have searched a lot to find a way for aggregation using loopback mongodb, unfortunately no perfect solution found. One of them is here But can't implement this, any one to help me solve this problem, with any new solution, or describing above link.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
jones
  • 1,423
  • 3
  • 35
  • 76

1 Answers1

0

Loopback doesn't provide a way to do an aggregation query, but you can find another solution in: https://github.com/strongloop/loopback/issues/890

//Using the datasource we are making a direct request to MongoDB instead of use the PersistedModel of Loopback
var bookCollection = Book.getDataSource().connector.collection(Book.modelName);

bookCollection.aggregate({
    $group: {
        _id: { category: "$category", author: "$author" },
        total: { $sum: 1 }
    }
}, function(err, groupByRecords) {
    if(err) {
        next(err);
    } else {
        next();
    }
});