Sequelize:查询 ARRAY 是否包含值
Sequelize: Querying if ARRAY contains a value
假设我有一个 PG ARRAY 字段:
id | array |
===|=============|
1|{"1","2","3"}|
如何使用sequelize查询数组字段是否为值1
.
我试过了:
array: { $contains: "1" }
这给了我:
array @> "1"
有错误:
Possibly unhandled SequelizeDatabaseError: array value must start with "{" or dimension information
更新
我能够通过以下方式做到这一点:
array: { $contains: '{' + value + '}' }
有没有更正确的方法?
我意识到 sequelize 期望条件是一个数组:
array: { [Op.contains]: ["1"] }
那行得通。干杯!!
请记住,Op
是从 sequelize
导出的
const { Op } = require('sequelize');
或
import { Op } from 'sequelize';
查看官方文档:https://sequelize.org/master/manual/model-querying-basics.html#operators
也许是这样。
`{ genres: { $contains: [genreType] } }`
genres
是一个数组。 genreType
也可以是数组
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
operatorsAliases: Sequelize.Op.Aliases,
});
假设我有一个 PG ARRAY 字段:
id | array |
===|=============|
1|{"1","2","3"}|
如何使用sequelize查询数组字段是否为值1
.
我试过了:
array: { $contains: "1" }
这给了我:
array @> "1"
有错误:
Possibly unhandled SequelizeDatabaseError: array value must start with "{" or dimension information
更新
我能够通过以下方式做到这一点: array: { $contains: '{' + value + '}' }
有没有更正确的方法?
我意识到 sequelize 期望条件是一个数组:
array: { [Op.contains]: ["1"] }
那行得通。干杯!!
请记住,Op
是从 sequelize
const { Op } = require('sequelize');
或
import { Op } from 'sequelize';
查看官方文档:https://sequelize.org/master/manual/model-querying-basics.html#operators
也许是这样。
`{ genres: { $contains: [genreType] } }`
genres
是一个数组。 genreType
也可以是数组
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
operatorsAliases: Sequelize.Op.Aliases,
});