[英]Sequelize: where query string is in array of strings postgresql
我正在嘗試按sequelize
執行查詢,在此我只希望獲得具有正確角色的用戶。 角色存儲為字符串數組。 例如['student']
或['school_owner', 'admin']
。
在這種情況下,我實際上是在嘗試建立一所學校,並包括該學校的學校所有者。 失敗的相關查詢是
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}
類的東西。 該文檔說,我可以在$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
查詢的正確方法,以便我可以匹配具有特定角色的所有用戶?
實現此功能的正確方法是執行以下操作
ownerQuery.roles = { $contains: ["school_owner"] };
這將返回所有在其roles
數組中具有school_owner
roles
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.