Bookshelf.js与json列Postgresql在哪里

Jon*_*zot 6 postgresql json bookshelf.js

我有一个Bookshelf的问题,我想使用查询列json类型我的表有列'数据'类型json,我想要在这列'team'='PSG'中获取所有元素

我测试:

  collection.query('whereRaw', "data->'team'->>'PSG'");
Run Code Online (Sandbox Code Playgroud)

我有这个错误

"WHERE的参数必须是boolean类型,而不是类型文本"

或者我测试一下

collection.query('where', "data", "#>", "'{team, PSG}'");
Run Code Online (Sandbox Code Playgroud)

我有这个错误

"不允许运营商"#"\"

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

Muh*_*min 4

简短回答:

collection.query('where', 'data', '@>', '{"team": "PSG"}');
Run Code Online (Sandbox Code Playgroud)

解释:

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

 ------------------------
| id | attr              |
 ------------------------
| 0  |{ "bar": "fooBar"} | 
 ------------------------
| 1  |{ "bar": "fooFoo"} | 
 ------------------------
Run Code Online (Sandbox Code Playgroud)

原始查询就像这样。

select * from "foos" where "attr" @> '{"bar":"fooBar"}';
Run Code Online (Sandbox Code Playgroud)

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

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

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

collection.query('where', 'data', '@>', '{"team": "PSG"}');
Run Code Online (Sandbox Code Playgroud)

希望兹拉坦批准它。