jsonb

Searching jsonb array in PostgreSQL

久未见 提交于 2019-11-30 15:50:28
问题 I'm trying to search a JSONB object in PostgreSQL 9.4. My question is similar to this thread. However my data structure is slightly different which is causing me problems. My data structure is like: [ {"id":1, "msg":"testing"} {"id":2, "msg":"tested"} {"id":3, "msg":"nothing"} ] and I want to search for matching objects in that array by msg (RegEx, LIKE, =, etc). To be more specific, I want all rows in the table where the JSONB field has an object with a "msg" that matches my request. The

Create Postgres JSONB Index on Array Sub-Object

放肆的年华 提交于 2019-11-30 15:45:03
I have table myTable with a JSONB column myJsonb with a data structure that I want to index like: { "myArray": [ { "subItem": { "email": "bar@bar.com" } }, { "subItem": { "email": "foo@foo.com" } } ] } I want to run indexed queries on email like: SELECT * FROM mytable WHERE 'foo@foo.com' IN ( SELECT lower( jsonb_array_elements(myjsonb -> 'myArray') -> 'subItem' ->> 'email' ) ); How do I create a Postgres JSONB index for that? If you don't need the lower() in there, the query can be simple and efficient: SELECT * FROM mytable WHERE myjsonb -> 'myArray' @> '[{"subItem": {"email": "foo@foo.com"}}

JSONb dates: actual dates internally?

烂漫一生 提交于 2019-11-30 08:36:08
问题 I'm using the postgresql jdbc adapter to migrate a bunch of data into a jsonb field (postgres 9.4). After importing, date fields look correct but are displayed surrounded with doublequotes. Is there a way to tell if they are actually stored internally as date values ? If they're strings, I don't think range lookups will be very efficient. For example, an entry in a properties jsonb field looks like this: "founded_on": "Sep 1, 2012 12:00:00 AM", I can now search on, say, SELECT CAST(properties

Merging Concatenating JSON(B) columns in query

非 Y 不嫁゛ 提交于 2019-11-30 03:13:50
Using Postgres 9.4, I am looking for a way to merge two (or more) json or jsonb columns in a query. Consider the following table as an example: id | json1 | json2 ---------------------------------------- 1 | {'a':'b'} | {'c':'d'} 2 | {'a1':'b2'} | {'f':{'g' : 'h'}} Is it possible to have the query return the following: id | json ---------------------------------------- 1 | {'a':'b', 'c':'d'} 2 | {'a1':'b2', 'f':{'g' : 'h'}} Unfortunately, I can't define a function as described here . Is this possible with a "traditional" query? Here is the complete list of build-in functions that can be used

Create Postgres JSONB Index on Array Sub-Object

天涯浪子 提交于 2019-11-29 23:01:09
问题 I have table myTable with a JSONB column myJsonb with a data structure that I want to index like: { "myArray": [ { "subItem": { "email": "bar@bar.com" } }, { "subItem": { "email": "foo@foo.com" } } ] } I want to run indexed queries on email like: SELECT * FROM mytable WHERE 'foo@foo.com' IN ( SELECT lower( jsonb_array_elements(myjsonb -> 'myArray') -> 'subItem' ->> 'email' ) ); How do I create a Postgres JSONB index for that? 回答1: If you don't need the lower() in there, the query can be

Query jsonb column containing array of JSON objects

廉价感情. 提交于 2019-11-29 13:59:57
I use PostgreSQL 9.5 and Rails 5. I want to query the jsonb column shown below that holds an array of JSON objects to return all the JSON array element containing {"kind":"person"} and also perform a count. The SQL I use is shown below the json data. Running the query just returns an empty array. I have tried the queries suggested here and here . This is what my jsonb data looks like: '[ {"kind":"person", "filter_term":"56","selected_attr":"customer"}, {"kind":"email", "filter_term":"marketer","selected_attr":"job_title"} ]' I want one of the sql query to return: data -------------------------

Postgres 9.4 jsonb array as table

被刻印的时光 ゝ 提交于 2019-11-29 11:41:49
I have a json array with around 1000 elements of the structure "oid: aaa, instance:bbb, value:ccc". {"_id": 37637070 , "data": [{"oid": "11.5.15.1.4", "value": "1", "instance": "1.1.4"} , {"oid": "11.5.15.1.9", "value": "17", "instance": "1.1.4"} , {"oid": "12.5.15.1.5", "value": "0.0.0.0", "instance": "0"}]} oid and instance are unique per json array. If I was given the option to change the structure I would have changed the format to key:value : {"11.5.15.1.4-1.1.4":"1", "11.5.15.1.9-1.1.4": "17", "12.5.15.1.5-0": "0.0.0.0"} However, if I need to stay with the old structure What is the

How to escape question mark (?) character with Spring JpaRepository

帅比萌擦擦* 提交于 2019-11-29 07:25:28
Postgres defines additional jsonb Operators such as ?| . However, using Spring JpaRepository query builder, interrogation character is always considered as a parameter, and I can't figure how to escape it (except inside a single quote string, but then the query is invalid). Example: @Query(value = "SELECT * FROM public.user u WHERE u.authorities ?| array['ROLE_1', 'ROLE_2']", nativeQuery = true) Error: java.lang.IllegalArgumentException: Unable to resolve given parameter name [1] to QueryParameter reference at org.hibernate.query.internal.QueryParameterBindingsImpl.resolveQueryParameter

JSONb dates: actual dates internally?

江枫思渺然 提交于 2019-11-29 07:21:39
I'm using the postgresql jdbc adapter to migrate a bunch of data into a jsonb field (postgres 9.4). After importing, date fields look correct but are displayed surrounded with doublequotes. Is there a way to tell if they are actually stored internally as date values ? If they're strings, I don't think range lookups will be very efficient. For example, an entry in a properties jsonb field looks like this: "founded_on": "Sep 1, 2012 12:00:00 AM", I can now search on, say, SELECT CAST(properties->>'founded_on' AS DATE and SELECT extract('year' from cast(properties->>'founded_on' as timestamp))

How to convert PostgreSQL 9.4's jsonb type to float

Deadly 提交于 2019-11-29 04:50:44
问题 I'm trying the following query: SELECT (json_data->'position'->'lat') + 1.0 AS lat FROM updates LIMIT 5; (The +1.0 is just there to force conversion to float. My actual queries are far more complex, this query is just a test case for the problem.) I get the error: ERROR: operator does not exist: jsonb + numeric If I add in explicit casting: SELECT (json_data->'position'->'lat')::float + 1.0 AS lat FROM updates LIMIT 5; the error becomes: ERROR: operator does not exist: jsonb + double