gpt4 book ai didi

json - Bookshelf.js 哪里有 json 列 Postgresql

转载 作者:行者123 更新时间:2023-11-29 11:25:12 25 4
gpt4 key购买 nike

我对 Bookshelf 有疑问,我想对 json 类型的列使用查询我的表有列 'data' 类型的 json,我想在此列 'team' = 'PSG' 中获取所有元素

我测试:

  collection.query('whereRaw', "data->'team'->>'PSG'");

我有这个错误

"argument of WHERE must be type boolean, not type text"

或者我测试

collection.query('where', "data", "#>", "'{team, PSG}'");

我有这个错误

"The operator \"#>\" is not permitted"

我认为有一份报告 https://github.com/tgriesser/bookshelf/issues/550

最佳答案

简答:

collection.query('where', 'data', '@>', '{"team": "PSG"}');

解释:

假设你有一个表 foos,其中一个元素 foo

 ------------------------
| id | attr |
------------------------
| 0 |{ "bar": "fooBar"} |
------------------------
| 1 |{ "bar": "fooFoo"} |
------------------------

原始查询就像这样。

select * from "foos" where "attr" @> '{"bar":"fooBar"}';

现在在 Bookshelf 中,如果您有一个代表表 foo 的模型 Foo,那么查询应该是这样的。

Foo.where('attr', '@>', '{"bar":"fooBar"}').fetch().then(function(rows){});

现在你的情况应该是这样的

collection.query('where', 'data', '@>', '{"team": "PSG"}');

希望 Zlatan 批准它。

关于json - Bookshelf.js 哪里有 json 列 Postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27780117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com