/assets/?tags[]=foo&tags[]=bar
对上述端点的获取请求应仅返回包含所有提供的标签( foo
、 bar
)的记录我目前的尝试是返回与任何给定标签匹配的任何记录。
const { tags } = req.query;
res.send(
await Asset.findAll({
where: whereOptions,
include: [
{ model: AssetMime },
{
model: Tag,
as: 'tags',
where: {
title: {
[Op.in]: tags,
},
},
},
],
})
);
如何修改我的过滤器以返回所有标签匹配的记录?
最佳答案
如果您的 DBMS 支持 Op.all
运算符,您可以尝试 op.in
运算符而不是 ALL
。
另一种解决方案是结合这些条件
const where = {}
if (tags && tags.length) {
where[Op.and] = tags.map(x => ({ title: x })
}
await Asset.findAll({
where: whereOptions,
include: [
{ model: AssetMime },
{
model: Tag,
as: 'tags',
where: where,
},
],
})
关于express - Sequelize - 通过匹配所有标签来过滤 FindAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63631187/