帆/水线:通过协会查找

时间:2016-01-14 12:57:27

标签: sails.js

如何通过集合关联查找并获取该查询的所有结果? 示例:

POSTS MODEL

module.exports = {

    attributes: {
      title:        'string',
      content:      'string',
      coverImage:   'string',
      owner: {
          model: 'user'
      },
      tags: {
          collection: 'ContentTag',
                 via: 'posts',
            dominant: true
      }
    }

};

TAGS MODEL

module.exports = {

  attributes: {
      name: 'string',
      posts: {
          collection: 'post',
          via: 'tags'
      }
  }
};

我需要:

*POSTS.find( { 
   where: {
     tags: 1 
   } 
}).populateAll() // All Post with ID tag 1*

不使用 TAGS.find({id:1})

1 个答案:

答案 0 :(得分:0)

不确定您是否可以按照自己的方式进行操作。但是,你可以做的是:

TAGS.find({id: 1})
.populate('posts')
.then(function(tags) {
    var posts = tags.posts
    // 'posts' now has all posts associated with tags with id 1
})