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

眉间皱痕 提交于 2019-12-14 00:45:50

问题


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

回答1:


Use

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



回答2:


Example override toJSON

    // Your Model
    module.exports = {
        attributes: {
            // some attributes here
            name: 'string',
            email: 'string',
            password: 'string',

            // Override .toJSON instance method
            toJSON: function() {
                var obj = this.toObject();
                delete obj.password;
                return obj;
           }
        }
    };



回答3:


Use this :

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



回答4:


In sails-mongo, for sorting ASC and DESC we can use same like mongo, For an example, if you want to fetch count in DESC order then the query is like,

Model.find({
 sort: {
    count: 0(Note:- Here 0 for DESC and 1 for ASC)
  }
})

Hope it will work for you.



来源:https://stackoverflow.com/questions/24539468/sails-js-waterline-orm-limit-or-sort-after-group-by

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