对于单个模型查询,我们可以对查询中定义的条件应用条件操作,其中 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/