如何在关联模型中执行find()/ where()

时间:2014-04-06 17:25:19

标签: node.js sails.js waterline

我在帆中玩模型关联,如果可以根据相关字段进行查询,我很好奇。

示例:

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

有没有办法根据类的类型检索Student的特定类,因为现在使用.populate()时返回了所有内容。 (可能与下面的逻辑类似)

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

由于

1 个答案:

答案 0 :(得分:0)

您可以向where添加populate条款,如下所示:

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

除了where之外,您还可以在skip的第二个参数中使用limitsortpopulate

请注意,这仍然是(在发布时)测试版,所以如果您发现任何问题似乎无法正常工作,请将其发布到Waterline GitHub issues forum