Sails js - 在 group by 后如何使用 limit 或者 sort?

9
我正在使用Waterline ORM来操作sails.js。当你使用groupby时,limitsort不能正常工作,但是如果不进行分组,它们可以正常工作。
例如:
Model.find({
    groupBy:['term'],
    sum:['count'],
    limit:20,
    sort :'count DESC'}).exec(function(error,response){
        if(error) res.json(error);
        res.json(response);
    });
4个回答

10

使用

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

在使用groupBy之前使用sort,也许会起作用。我希望上述方法没有出现任何错误,对吧? - Mahammad Adil Azeem
我也尝试过那个方法。你的方法没有报错,只是显示了所有结果。 - sanath_p
这意味着您没有应用正确的过滤器。您能否更新您的问题,并提供您的模型和控制器文件,即 api/models/Foo.jsapi/controller/whateverController.js - Mahammad Adil Azeem
尝试使用1/-1替换'asc'/'desc',例如:.sort({count: -1}). - Karma
在v1+中不支持groupBy。请参阅官方文档https://sailsjs.com/documentation/upgrading/to-v-1-0#changes-to-waterline-criteria-usage - Gopal Kohli
显示剩余2条评论

0

示例:重写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;
           }
        }
    };

0

使用这个:

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

0
在 sails-mongo 中,排序 ASC 和 DESC 可以使用与 mongo 相同的方式, 例如,如果您想以 DESC 的顺序获取计数,则查询如下:
Model.find({
 sort: {
    count: 0(Note:- Here 0 for DESC and 1 for ASC)
  }
})

希望它能为您工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接