是否可以在sails中查询关联的字段属性。例如,让我们考虑这两个模型:
产品:
attributes: {
name:{
type:'string'
},
code:{
type:'string',
required:true,
unique:true
},
inventory:{
collection:'inventory',
via:'product'
}
}
广告
attributes: {
product:{
model:'product',
required:true
},
quantity:{
type:'float'
}
}
现在有办法获得Inventory
某个Products
codes
的记录。一种方法是获取那些特定products
的{{1}},然后获取具有codes
的{{1}}条记录。但是,是否可以执行单个Inventory
查询以获得所需结果?我尝试过关注,但这不会在products
上过滤find
。
products
答案 0 :(得分:1)
事实上,根据您的请求,您将获取所有广告资源并仅填充code:{'contains':'something'}
其他广告资源的产品将包含空产品。
您可以双向进行关联,并执行此操作:
Product
.find().where({code:{'contains':'something'}})
.populate('inventory')
.exec(function (err, products) {
//do something
//Build an array of inventory with async on all products
})
据我所知,实际上没有其他方法可以做到这一点:/
答案 1 :(得分:1)
不,你不能。你有最好的选择 - 永远不会使用风帆!