Sequelize:查询 ARRAY 是否包含值 [英] Sequelize: Querying if ARRAY contains a value
本文介绍了Sequelize:查询 ARRAY 是否包含值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个 PG ARRAY 字段:
Suppose I have a PG ARRAY field:
id | array |
===|=============|
1|{"1","2","3"}|
如何使用sequelize查询数组字段是否为值1
.
How do I use sequelize to query to see if the array field as the value 1
.
我试过了:
array: { $contains: "1" }
这给了我:
array @> "1"
有错误:
Possibly unhandled SequelizeDatabaseError: array value must start with "{" or dimension information
更新
我能够通过以下方式做到这一点:数组:{ $contains: '{' + value + '}' }
UPDATE
I was able to do it by: array: { $contains: '{' + value + '}' }
有没有更正确的方法?
推荐答案
我意识到 sequelize 期望条件是一个数组:
I realised that sequelize is expecting the condition to be an array:
array: { [Op.contains]: ["1"] }
那行得通.干杯!!
请记住,Op
是从 sequelize
const { Op } = require('sequelize');
这篇关于Sequelize:查询 ARRAY 是否包含值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文