问题
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