2016-09-30 78 views
0

我们有回环中的数据模型,其中客户将与订单具有hasMany关系,并且当我查询客户时,订单将出现在该特定客户的数组中,并且如果我想使用由给定的现有选项回环什么是我需要使用查询来获得特定命令名称在AND条件下的所有客户选项。环回关系模型查询

在此先感谢您的帮助。

回答

0

我认为你正在寻找的过滤器[include]功能: http://loopback.io/doc/en/lb2/Include-filter.html

问题我在这里发现不支持外键。您可以在模型本身内定义组合(多个字段)主键,但无法基于组合键{field1,field2,...]自定义替代方法来定义关系。

0

你可以查询,但它会像SQL一样左连接。

通过以下查询:

Customer.find({ 
    where:{}, 
    include: { 
    relation: 'orders', 
    scope: { 
     where: { 
      name: 'order name' 
     } 
    } 
    } 
}, (err, callback)) 

结果:

customers array with orders as array 
ex: [ 
    { 
     name: customer A, 
     orders: [{...},{...}] 
    }, 
    { 
     name: customer B, 
     orders: [] //That does not match condition in scope 
    } 
    ] 

MoreInfo:Loopback doc reference

但是,如果你想要的是再使用本地蒙戈查询:

CustomerCollection = Customer.getDataSource().connector.collection("Customer"); 

and fire aggregate(Native mongo query)