Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql json like query

I have the following table called module_data. Currently it has three rows of entries:

                id                               data 0ab5203b-9157-4934-8aba-1512afb0abd0 {"title":"Board of Supervisors Meeting","id":"1i3Ytw1mw98"} 7ee33a18-63da-4432-8967-bde5a44347a0 {"title":"Board of Supervisors Meeting","id":"4-dNAg2mn6o"} 8d71ca35-74eb-4751-b635-114bf04843f1 {"title":"COPD 101", "id":"l9O0jCR-sxg"} 

Column data's datatype is jsonb. I'm trying to query it using like operator. Something like the following:

SELECT * FROM module_data WHERE title LIKE '%Board%'; 

I've been looking at the jsonb support and there doesn't seem to be a like operator. If anyone has any advice.

like image 749
adviner Avatar asked Mar 21 '17 04:03

adviner


People also ask

What is Postgres JSON query?

Postgres is a relational database that allows you to combine relational and non-relational data effortlessly, giving users/applications flexibility in accessing and handling the data. Additionally, Postgres includes native support for querying and processing JSON data. Read along to learn more about Postgres JSON Query. What is JSON Data?

What is jsonpath in PostgreSQL?

In PostgreSQL, path expressions are implemented as the jsonpath data type and can use any elements described in Section 8.14.7. JSON query functions and operators pass the provided path expression to the path engine for evaluation. If the expression matches the queried JSON data, the corresponding JSON item, or set of items, is returned.

How do I update a JSONB column in PostgreSQL?

2. Basics of PostgreSQL's JSONB data type We'll first look at some basic operations for inserting and updating JSONB columns. Use the || operator to concatenate existing data with new data. The operator will either update or insert the key to the existing document.

How to display multiple rows in a JSON column in PostgreSQL?

The result has been that all of the JSON data has been displayed in a single column. PostgreSQL includes some functions that let you expand the JSON fields and display them in different rows. The JSONB_EACH function will display each key and value pair as elements. It will split each key into separate rows.


1 Answers

If the data column is text type, then use ->> on cast:

select * from module_data where data::json->>'title' like '%Board%' 

If it's already json:

select * from module_data where data->>'title' like '%Board%' 
like image 196
Gurwinder Singh Avatar answered Sep 30 '22 03:09

Gurwinder Singh