Sort by the association with populate

…衆ロ難τιáo~ 提交于 2019-12-01 07:34:10
Melvin

You pass it in the second parameter of .populate() like this:

.populate('foo', { sort: 'comments ASC' }).exec(...)

Neither waterline (0.10.22) or sailsjs (v0.11) currently support this. You would need to process your return variable to reorder the data.

There is a ticket for this at https://github.com/balderdashy/waterline/issues/334

There is a difference between

 function mostCommentedArticles () {
  var deferred = Q.defer();
  Article.find().populate('comments').sort('comments.comment_date ASC').exec(deferred.makeNodeResolver());
  return deferred.promise;
 }

and

function mostCommentedArticles () {
  var deferred = Q.defer();
  Article.find().populate('comments', {sort: 'comment_date ASC'}).exec(deferred.makeNodeResolver());
  return deferred.promise;
 }

The first one should return all the articles and comments with everything sorted by the comment_date. The second one should return all your articles with the attached comments where the comments are sorted by date.

The second one should work using mongodb.

This might point you in the right direction:

.populate('foo', { where: [ '1' ]);

You can find more on the where operator here

But be aware that you for now won't be able to easily filter by both a model's native fields, and of populated fields - issue on hithub.

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