Skip to content

Instantly share code, notes, and snippets.

@jaklinger
Last active May 8, 2018 10:35
Show Gist options
  • Save jaklinger/e2a6c796e781c1c1ac55f0b7e045b48c to your computer and use it in GitHub Desktop.
Save jaklinger/e2a6c796e781c1c1ac55f0b7e045b48c to your computer and use it in GitHub Desktop.
MAG postgresql jsonb cheatsheet
-- Selecting nested elements (e.g. author names)
select author -> 'name' as author_name
from microsoft_academic_graph
cross join jsonb_array_elements(microsoft_academic_graph.paper -> 'authors') author
limit 1;
-- Finding specific papers by author
select paper->'title', paper->'authors'
from microsoft_academic_graph
where paper->'authors' @> '[{"name":"John Smith"}]'
limit 1;
-- Finding indexed items. Note, my indexes are:
-- "doi_index" btree ((((paper ->> 'doi'::text))::character varying(255)))
-- "lang_index" btree ((paper ->> 'lang'::text))
-- "papers_authors_name_gin_idx" gin ((paper -> 'authors'::text) jsonb_path_ops)
-- "title_index" btree ((((paper ->> 'title'::text))::character varying(300)))
-- "year_index" btree (((paper ->> 'year'::text)::integer))
select paper->>'title'
from microsoft_academic_graph
where ((((paper ->> 'doi'::text))::character varying(255))) = '10.1088/1748-0221/11/07/P07010'
limit 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment