json - 在sequelize where子句中使用ILIKE(JSON列)

标签 json node.js postgresql sequelize.js

我有 JSON 列,其中存储了类似 - 的数据

{ tag : ["as","bs","cs"] }

我想使用 ILIKE 在此列中搜索,并且我相信 JSON 数据类型只是字符串,所以我使用了类似的查询 -

SELECT * FROM public."Transactions" WHERE tags::text ILIKE '%as%'

上面的查询在 sql 中运行良好

我需要用sequelize模型来实现这个但没有成功 我使用的代码是

    let searchQuery = [
        {
            payee: {
                [Op.iLike]: '%' + search + '%'
            }
        },
        {
            tags: {
                [Op.iLike]: '%as%'
            }
        }
    ];

给出错误

Unhandled rejection SequelizeDatabaseError: operator does not exist: json ~~* unknown

最佳答案

https://www.postgresql.org/docs/current/static/functions-json.html

检查运算符列表 - 没有 ~~ - ILIKE 不适用于 json。您必须将 json 转换为文本:

t=# select '{ "tag" : ["as","bs","cs"] }'::json::text ilike '%as%';
 ?column?
----------
 t
(1 row)

或使用 native 运算符:

t=# select ('{ "tag" : ["as","bs","cs"] }'::json)->'tag'->>0 = 'as';
 ?column?
----------
 t
(1 row)

或者如果您是 9.5 及以上版本 - 转换为 jsonb 并使用其强大的运算符:

t=# select '{ "tag" : ["as","bs","cs"] }'::json::jsonb @> '{"tag":["as"]}'::jsonb;
 ?column?
----------
 t
(1 row)

https://www.postgresql.org/docs/current/static/functions-json.html

关于json - 在sequelize where子句中使用ILIKE(JSON列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48536774/

相关文章:

javascript - 如何在javascript中检查对象中所有级别的对象是否为空

javascript - 在 Browserify 中制作 jQuery、Backbone 等全局变量

javascript - 为什么 Node REPL 根据是否分配给变量对表达式进行不同的计算?

c++ - 无法使用 node-gyp (C++) 编译 STK (The Synthesis Toolkit)

sql - 按加入分组并计算组的最大值

json - 解析Amazon Electronics评论Apache Pig

javascript - Kendo UI 网格服务器端分组(无 MVC)

java - 将 JSON 解析为 java 对象

postgresql - 使用 postgres uuid_generate_v4 时有很多重复值

postgresql - pg_config - 如何更改 postgres 版本