首页 文章

环回关系模型查询

提问于
浏览
0

我们在环回中有数据模型,其中客户将与订单具有hasMany关系,当我查询客户时,订单将以特定客户的数组形式出现,如果我想使用环回给出的现有选项,我需要什么选项用于查询以获取AND条件中具有特定订单名称的所有客户 .

在此先感谢您的帮助 .

3 回答

  • 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

    但如果你想要完全然后使用本机mongo查询:

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

    和fire aggregate(Native mongo query)

  • 0

    如果我理解正确,你的意思是 Customers.find({where: {orderId = 1}}, function (err, cb) ??

    如果你想使用和/或条件,这是一个例子:

    Post.find({where: {and: [{title: 'My Post'}, {content: 'Hello'}]}}, 
              function (err, posts) {
                ...
    });
    

    您可以在此链接中找到更多信息:https://docs.strongloop.com/display/public/LB/Where+filter#Wherefilter-and/or

  • 0

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

    我在这里找到的问题不支持外键 . 您可以在模型本身中定义复合(多个字段)主键,但不能基于复合键{field1,field2,...]定制关系,自定义解决方法 .

相关问题