按与 populate 的关联排序 [英] Sort by the association with populate

查看:63
本文介绍了按与 populate 的关联排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过一对多关联链接的文章和评论(一篇文章可以有多个评论).

I have Articles and Comments linked by a one-to-many association (an Article can have many Comments).

我想获得评论最多的文章,所以我这样做:

I would like to obtain the most commented articles so I proceed like that :

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

但是,我没有得到预期的结果:它根本没有排序(按评论或其他任何方式)

BUT, I don't obtain the expected result : it doesn't sort at all (by Comments or anything else)

是否有其他方法可以继续操作或是否存在问题?

Is there an other way to proceed or is it an issue?

谢谢,

皮埃尔

推荐答案

你把它传入 .populate() 的第二个参数中,像这样:

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

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

这篇关于按与 populate 的关联排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆