我在 psql 数据库中有一个表,如下所示。
[{
name="ABC",
email="xyz",
animals=["cats","dogs","elephants","giraffes"]
},
{
name="BCD",
email="fgh",
animals=["giraffes"]
}]
....
以及我将在请求中获取的数组,如下所示
hasAnimals=["lion","cheetah","dog","cat"]
如何使用sequelize where子句来获取上表中所有拥有hasAnimals数组中至少1只动物的人的记录。
例如。上述示例应该为我获取名为 ABC 的用户,因为 ABC 有猫和狗,我在请求中获得的数组也是如此
最佳答案
事实证明,解决方案非常简单。
const { Op } = require("sequelize");
let arr = []
for(let animal of animals) {
arr.push({
[Op.contains]: [animal],
});
}
abc.findAll({
where: {
animals: {
[Op.or]: arr
}
},
})
关于node.js - Sequelize : How to match atleast 1 value from array stored in db with array in the request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69893770/