【问题标题】:Loopback.io Multiple Include Model Query ConditionsLoopback.io 多重包含模型查询条件
【发布时间】:2015-06-21 11:24:01
【问题描述】:

对于单个模型查询,我们可以对查询中定义的条件应用条件运算,其中 and/or 运算符应用于一系列条件:

{ where: {<and|or>: [condition1, condition2, ...]}}

是否有一种巧妙的方法可以为 include 中包含的模型应用条件运算?

举个例子:

Model.find({
    include: [
        {
            relation: "relation1",
            scope: {
                where: { condition1 }
            }
        },
         {
            relation: "relation2",
            scope: {
                where: { condition2 }
            }
        },
        ...
    ]
});

是否可以对上面的condition1,condition2应用和/或操作,即返回所有模型,使其与包含模型范围内定义的条件相匹配。

【问题讨论】:

    标签: javascript node.js loopbackjs


    【解决方案1】:

    我相信您可以按照以下代码示例执行此操作:

    Post.find({
      include: {
        relation: 'owner', // include the owner object
        scope: { // further filter the owner object
          fields: ['username', 'email'], // only show two fields
          include: { // include orders for the owner
            relation: 'orders', 
            scope: {
              where: {orderId: 5} // only select order with id 5
            }
          }
        }
      }
    }, function() { ... });
    

    这已记录在 here 用于包含过滤器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 2020-05-09
      相关资源
      最近更新 更多