Last active
November 11, 2023 10:57
-
-
Save nilsmagnus/8302c63c0252101ce3e5d479a493c124 to your computer and use it in GitHub Desktop.
postgres med json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into artikler(article_type, content) | |
values (1, '{"articleId": "xyz123", "meta":{"tag":"bil", "title":"ny elbil", "codes":["a","b"]}}'::jsonb); | |
select content::text | |
from artikler; | |
select content -> 'meta' ->> 'title' as tittel | |
from artikler | |
where content -> 'meta' ->> 'tag' = 'bil'; | |
select id, id, jsonb_array_elements(content -> 'meta' -> 'codes') | |
from artikler | |
where content @> '{"meta":{"codes":["a"]}}'; | |
create index article_tag_idx on artikler using gin ((content -> 'meta' -> 'tag')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker start jsonbtesting || docker run -e POSTGRES_USER=jsonb_user -e POSTGRES_PASSWORD=dingafu -e POSTGRES_DB=jsonbtesting --name jsonbtesting -dt -p 5432:5432 postgres:15.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment