0
myTable
{
 "creator": "[email protected]",
 "data": {
  "sections" : [],
  "sharing": [
    {
     "key": "value",
      "user": "[email protected]"
    },
    {
     "key": "value",
     "user": "[email protected]"
    },
    {
     "key": "value",
     "user": "[email protected]"
    }
   }
}

I have above table stored in postgres, and data column stored in jsonb format, I want to query with user email eg. user : [email protected]

I have tried below query but the response is slow

SELECT creator, data - 'sections' AS data FROM myTable WHERE (data->'sharing')::text LIKE '%[email protected]%' ORDER BY xmin::text::bigint DESC

1 Answer 1

1

Use the contains operator @>

select ..
from mytable
where data -> 'sharing' @> '[{"user": "[email protected]"}]'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but this is not working its not returning anything SELECT creator, data FROM myTable where data->'sharing' @> '{"user" : "[email protected]"}'
Ah, sorry. The parameter also needs to be an array. See my edit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.