Last active
October 25, 2021 19:02
-
-
Save filipedfs/a3661d1a91694740bd4aaf14203f8ae9 to your computer and use it in GitHub Desktop.
Postgres query to search inside jsonb
This file contains hidden or 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
| -- {"contacts":[{"phoneNumber":987654321}]} See: https://www.postgresql.org/docs/12/functions-json.html | |
| SELECT contacts | |
| FROM customer | |
| WHERE contacts @? '$[*] ? (@.phoneNumber == 987654321)'; | |
| -- Other way | |
| WITH data AS ( | |
| SELECT jsonb_array_elements(contacts)->>'phoneNumber' "phoneNumber" | |
| FROM customer | |
| ) | |
| SELECT * FROM data WHERE "phoneNumber" = '11985594060'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment