Home > Software design >  LIKE in Array of Objects in JSONB column
LIKE in Array of Objects in JSONB column

Time:07-12

I have JSONB data in a Postgres column like this:

{
  "Id": "5c6d3210-1def-489b-badd-2bcc4a1cda28",
  "Name": "Jane Doe",
  "Tags": [
    {
      "Key": "Project",
      "Value": "1004345"
    }
  ]
}

How can I query data where Name contains "Jane" or "Tags.Key" contains "4345"?

I tried this but this only matches the exact "Key" value:

select * from documents where data->'Tags' @> '[{ "Value":"1004345"}]';

CodePudding user response:

You can use a JSON path operator using like_regex

select *
from documents
where data @@ '$.Tags[*].Value like_regex "4345"'

CodePudding user response:

  • you can do this way
select *
from documents
where 'Tags' ->> 'Value' = '1004345';
  • Related