6

I have a problem with Bookshelf, I want use query to columns json type My table have columns 'data' type json, I want get all element with in this columns 'team' = 'PSG'

I test:

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

I have this error

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

Or I test

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

I have this error

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

I think that have a report with https://github.com/tgriesser/bookshelf/issues/550

1 Answer 1

4

Short answer:

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

Explanation:

Say you have a table foos, where an element foo is

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

The raw query for that would be like.

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

Now in Bookshelf if you have a model Foo that represent table foos it the query should be like.

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

Now for your case it should be like

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

Hopefully Zlatan Approves it.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm using the same query, but still getting error, Unhandled rejection error: select "foo".* from "foo" where "attr" @> $1 limit $2 - operator does not exist: json @> unknown.
Can you please run your knex query with debug flag and post your exact query?
Whoever is trying to accomplish this same task, do not follow these instructions. Vasanth's comment from Oct 19 '16 still hold true. PostgreSQL generates the following error: ERROR: operator does not exist: json @> unknown LINE 1: ...t * from "users" where "email_subscription_prefs" @> '{"mark... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.