我試圖在sequelize
中執行查詢,我只想獲取具有正確角色的用戶。角色被存儲爲一個字符串數組。例如['student']
或['school_owner', 'admin']
。Sequelize:其中查詢字符串在字符串數組中postgresql
在這個特殊的情況下,我實際上是在爭取一所學校,並且包括那所學校的學校老闆。發生故障的相關查詢是
const ownerQuery: { [key: string]: any } = {};
ownerQuery.roles = {$in: ["{school_owner}"]};
School
.findById(req.params.id,{include: [{ model: User, where: ownerQuery }]})
.then((school: School | null) => {
if (school === null) {
res.status(404).json({message: "Not Found"})
} else {
res.json(school)
}
}, (error) => next(error))
sequelize
被存儲陣列值作爲類似{school_owner, admin}
。該documentation說,我可以改用下面我$in
查詢
ownerQuery.roles = {$in: ["school_owner"]};
其去除{}
,但它給了我一個Array value must start with "{" or dimension information.'
錯誤。
在第一個示例中,查詢不會失敗,但它不起作用,類似於查詢$in
。我必須準確匹配roles
的內容。例如,如果用戶同時具有admin
和school_owner
角色,我不得不說
ownerQuery.roles = {$in: ["{school_owner, admin}"]};
什麼是執行$in
查詢,以便我可以匹配具有特定角色的所有用戶的正確方法?