如何在sails的相关字段上查找查询
有没有可能在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' } }
现在有什么方法可以获得具有特定codes
Products
的Inventory
logging。 一种方法是获得这些特定codes
products
,然后获得具有这些products
Inventory
logging。 但有可能做一个查询查询来获得所需的结果吗? 我试过以下,但是这不会过滤code
products
。
Inventory .find() .populate('product',{where:{code:{'contains':'something'}}}) .exec(function (err, inventories) { //do something })
实际上,如果您的请求采用了所有库存,并且只填充了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 })
据我所知,实际上没有其他办法可以做到这一点:
不,你不能。 最好的select,你有 – 它不是使用帆!