B
B
Ba An Doan2020-08-04 22:31:15
PostgreSQL
Ba An Doan, 2020-08-04 22:31:15

How to search table records by JSONB type field?

There is Laravel 7 and PostgresSQL 11 DBMS. It has a users table . The users table has a contacts field of type JSONB . The data structure in the contacts field is as follows:

{
    "phones": [
        { "value": "89999999999" },
        { "value": "81111111111" }
    ]
}


Is it possible to search for a record by value field using whereJsonContains ? If not, how can the problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tsvetkov, 2020-08-05
@doanbaanh

SELECT * FROM table where data->'phones' @> '[{"value":"81111111111"}]'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question