0

Case 1 (works fine) : my db entry is a simple object

employee_id | data (jsonb)
4           | {"name":"john"}

SELECT * 
FROM employee
WHERE data ? 'john'; 

It will correctly find the whole entry

Case 2 : my db entry is an array of elements

employee_id | data
4           | [{"name":"john"},{"city":"rio"}]

I can't manage to use a jsonb function to find rows that match 'john' anywhere. At best I can use WHERE data->0 ? 'john' but this is not supposed to depend on the index of the array entry

Thx!

1
  • 2
    Your "working" example doesn't because 'john' is not the key. Commented Nov 29, 2019 at 12:48

1 Answer 1

1

You can use the jsonb “contains” operator @> like this:

... WHERE data @> '[{"name": "john"}]'

That will find all rows where data has an array on the top level that contains this entry.

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

Comments

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.