Sails js - waterline ORM limit or sort after group by?

sanath_p picture sanath_p · Jul 2, 2014 · Viewed 10.2k times · Source

I am using Waterline ORM for sails.js. limit and sort are not working when you use groupby, but are working fine when you dont do any grouping .

For example

Model.find({
    groupBy:['term'],
    sum:['count'],
    limit:20,
    sort :'count DESC'}).exec(function(error,response){
        if(error) res.json(error);
        res.json(response);
    });

Answer

Adil Malik picture Adil Malik · Jul 3, 2014

Use

Model.find()  
 .groupBy('term') 
 .sum('count')  
 .limit(20)
 .sort({count: 'desc'}) 
 .exec(function (err, data){
 //Your code here..
});