javascript - Loopback.io 多重包含模型查询条件

标签 javascript node.js loopbackjs

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

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

有没有一种巧妙的方法可以为 include 中包含的模型应用条件操作?

举个例子:

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

是否可以对上述条件 1、条件 2 应用和/或操作,即返回所有模型,使它们匹配包含模型范围内定义的条件。

最佳答案

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

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对于包含过滤器

关于javascript - Loopback.io 多重包含模型查询条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963970/

相关文章:

javascript - 在文本输入字段中显示纬度和经度

javascript - 数据表翻译不起作用

javascript - 具有缩放效果的 jquery 事件(居中)幻灯片

node.js - 如何为heroku redis Node 启用tls?

node.js - 如何在 Loopback 4 中设置一个单独的 DTO 而不是 Persistable 模型

javascript - Chrome 问题 : "Failed to load resource: net::ERR_CONNECTION_TIMED_OUT "

mysql - 如何在不使用直接查询的情况下从 Node js中的mysql数据库中获取数据?

node.js - 尝试在 Express/NodeJS 中检索 url 元数据

node.js - 如何在 Heroku 上部署的 Loopback 应用程序中记录 HTTP JSON 请求?

json - LoopBack JSON 返回不一致