2015-01-05 31 views
6

我有一個Bookshelf的問題,我想使用查詢列json類型 我的表有'數據'類型的json類型,我想要在這個列'團隊'中獲取所有元素= 'PSG'Bookshelf.js與json列在哪裏Postgresql

我測試:

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

我有這樣的錯誤

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

或I測試

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

我有這樣的錯誤

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

我認爲有一個報告https://github.com/tgriesser/bookshelf/issues/550

回答

4

簡短的回答:

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.where('attr', '@>', '{"bar":"fooBar"}').fetch().then(function(rows){}); 

現在對於你的情況應該是這樣

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

希望伊布拉希莫批准它。

+0

我正在使用相同的查詢,但仍然出錯,'未處理的拒絕錯誤:從「foo」中選擇「foo」*,其中「attr」@> $ 1限制$ 2 - 運算符不存在:json @> unknown '。 – Vasanth

+0

你可以用調試標誌運行你的knex查詢併發布你的確切查詢嗎? –