Skip to content

Instantly share code, notes, and snippets.

@rusllonrails
Last active August 29, 2015 14:23
Show Gist options
  • Save rusllonrails/17288e3957622f864217 to your computer and use it in GitHub Desktop.
Save rusllonrails/17288e3957622f864217 to your computer and use it in GitHub Desktop.
POSTGRESQL JSONB USEFULL FUNCTIONS
1) Check if array 'risk_phrases' in 'doc' jsonb column is not empty
(doc -> 'risk_phrases')::text <> '[]'::text
2) Check if key 'name' in 'doc' jsonb column is not blank
(doc ->> 'name')::text <> '{}'::text
3) Check if array 'supplier_ids' in 'doc' jsonb column contain at least of one element from array ['bar', 'goo', 'foo']
(doc -> 'supplier_ids')::jsonb ?| array['bar', 'goo', 'foo']
4) Check if key 'type' in 'doc' jsonb column has defined "Vasa" value:
doc @> '{"type": "Vasa"}'
OR:
doc ->> 'type' = 'Vasa'
5) Nested check. Check if array 'ingredients' in 'doc' jsonb column at least of one element from with "ingredient_type" = 'Type1'
doc @> '{"ingredients": [{"ingredient_type": "Type1"}]}'
6) Check if key 'poison_schedule_id' in 'doc' jsonb column is one of elements from array ["cool", "things"]
'["cool", "things"]'::jsonb ? (doc ->> 'poison_schedule_id')
7) Equil in right side.
SELECT doc -> 'synonyms'
FROM justjsonb WHERE 's2|bar|baz' ~* 's';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment