MongoDB aggregation on Loopback
Asked Answered
H

3

6

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 having to find all the rows and sum it in Node.js.

UPDATE

Trying out the example from https://github.com/strongloop/loopback/issues/890

var bookCollection = Book.getDataSource().connector.collection(Book.modelName);

I got an error

throw new Error('MongoDB connection is not established');

How do I get a handle on the collection to manually run aggregate query on a MongoDB collection?

Hegumen answered 5/6, 2015 at 18:40 Comment(1)
Also saw this github.com/strongloop/loopback/issues/890 Anyone has any way to workaround this? Even if it's a non-loopback way.Hegumen
H
15

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);
  });
});
Hegumen answered 6/6, 2015 at 15:40 Comment(4)
It doesn't need to connect. My code works fine with the example on github.com/strongloop/loopback/issues/890Billbillabong
Thank you @uzyn! I think there are corner cases where the model may not be connected, and this is needed just in case.Birdbath
Hey, I got an error like aggregate is not function. Can you please help me out?Distance
what format has the data object in your function? when I try to log it in it is AggregateCoursor and I don't really know how to properly get the data out of it. And I see that you are passing that to the callback directly. does it work?Ahem
P
1

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

         });
    });
Phox answered 26/2, 2019 at 5:38 Comment(0)
O
0

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" }
    }}
  ]).toArray(function(err,servicesData){
                if(err){
                    return callback(err)
                 }else{
                    return callback(null,servicesData);   
             }
     });
});
Outoftheway answered 25/1 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.