2

我正在使用sails 中的模型关联,我很好奇是否可以根据关联字段进行查询。

例子:

User.js
 attributes:{
  classes: { collection: 'Class', via: 'students' }
 }
Class.js
 attributes: {
  type: ...
  students: { collection: 'User', via: 'classes'}
 }

有没有办法根据类的类型检索学生的特定类,因为现在我使用.populate(). (可能与下面的逻辑类似)

User
 .findOne({name: 'StudentA'})
 .populate('classes')
 .where({'classes.type':['type1', 'type2']})
 .then(....)

谢谢

4

1 回答 1

0

您可以像这样添加一个where子句populate

User
 .findOne({name: 'StudentA'})
 .populate('classes', {where: {type: ['type1', 'type2']}})
 .exec(...)

除此之外where,您还可以在第二个参数中使用skip,limit和。sortpopulate

请记住,这仍然(截至本文发布)处于测试阶段,因此如果您发现任何似乎无法正常工作的问题,请将它们发布到Waterline GitHub 问题论坛

于 2014-04-07T05:06:36.780 回答