I have a table uploads_table
:
upload_id | value |
---|---|
A | [{"doc_name": "doc1a", "doc_type": "pdf"}, {"doc_name": "doc1b", "doc_type": "csv"}] |
B | [{"doc_name": "doc2a", "doc_type": "csv"}, {"doc_name": "doc2b", "doc_type": "csv"}] |
C | [{"doc_name": "doc3a", "doc_type": "pdf"}] |
What would be the Postgres query to return all the upload_id, value
from uploads_table
which has at least one "doc_type" as "pdf".
Expected result:
upload_id | value |
---|---|
A | [{"doc_name": "doc1a", "doc_type": "pdf"}, {"doc_name": "doc1b", "doc_type": "csv"}] |
C | [{"doc_name": "doc3a", "doc_type": "pdf"}] |
2
Answers
You can use the built-in function jsonb_array_elements in PostgreSQL. The query statement will be as follows:
The result is as shown in the image below:
Use the
jsonb
"contains" operator@>
:This is, of course, assuming that
value
is typejsonb
– as it should be.A GIN index on
(value)
will make this fast.Even faster with a more specialized
jsonb_path_ops
index:See:
Related: