jsonb

JSONB array contains like OR and AND operators

三世轮回 提交于 2019-12-11 18:12:15
问题 Consider a table temp (jsondata jsonb) Postgres provides a way to query jsonb array object for contains check using SELECT jsondata FROM temp WHERE (jsondata->'properties'->'home') ? 'football' But, we can't use LIKE operator for array contains. One way to get LIKE in the array contains is using - SELECT jsondata FROM temp,jsonb_array_elements_text(temp.jsondata->'properties'->'home') WHERE value like '%foot%' OR operation with LIKE can be achieved by using - SELECT DISTINCT jsondata FROM

Postgres jsonb nested array append

人盡茶涼 提交于 2019-12-11 17:53:48
问题 I have simple table with a jsonb column CREATE TABLE things ( id SERIAL PRIMARY KEY, data jsonb ); with data that looks like: { "id": 1, "title": "thing", "things": [ { "title": "thing 1", "moreThings": [ { "title": "more thing 1" } ] } ] } So how do I append inside of a deeply nested array like moreThings ? For single level nested array I could do this and it works: UPDATE posts SET data = jsonb_set(data, '{things}', data->'things' || '{ "text": "thing" }', true); But the same doesn't work

JSON index much slower than index on text column with same value

拜拜、爱过 提交于 2019-12-11 17:10:02
问题 I have an index: CREATE INDEX index_c_profiles_on_city_state_name_domain ON c_profiles ((data->>'state'), (data->>'city'), name, domain); and another index: CREATE INDEX index_c_profiles_on_state ON c_profiles (state) with a new column I created called 'state' with the value of data->>'state' copied manually over from the jsonb column for each row I tried this query to use the first index: SELECT mm.name, mm.domain, mm.data ->> 'city' as city, mm.data ->> 'state' as state FROM c_profiles as

Update key value in jsonb array of objects

走远了吗. 提交于 2019-12-11 16:18:24
问题 I have the following table company with a jsonb column named log : - |code | log ----------------------------------------------------------------------------- |K50 | [{"date": "2002-02-06", "type": "Chg Name", "oldvalue": "TEH "}, {"date": "2003-08-26", "type": "Chg Name", "oldvalue": "TEOA "}] |C44 | [{"date": "2003-05-07", "type": "Chg Name", "oldvalue": "CDE "}] How to trim the trailing blanks in the oldvalue ? 回答1: You can do it with a mix of jsonb functions and operators: UPDATE company

Is there a way to generate columns in a view based on table row data?

和自甴很熟 提交于 2019-12-11 16:12:45
问题 I have this table which contains the settings of an app & I just want to show it in the view. The data of each setting is stored as a row. Code (varchar64)| Value (varchar1000) ---------------------- ALLOW_MAC_ADDR | 1 ---------------------- ALLOW_SAVE | 1 ---------------------- USER_ALIAS | James Now this is where it gets kinda complicated, I have to convert these rows into a jsonb at the view. The key for value column name has to be based on the value of the Code column data. Here is an

Updating Postgres 9.5 Jsonb by id

邮差的信 提交于 2019-12-11 13:51:45
问题 I've read the docs and stack, but I'm too dumb to get this without asking my specific question. Say I have an array of objects stored in a jsonb column, e.g. [{"id":1, "value":"a"], {"id":2, "value":"b"}] What is the most efficient way to change index 1's value from "b" to "c" if you don't have the index and must search by id=2? To be more specific, I'm writing a react/redux/node real time app, and I don't want to trust the redux state to give the index to update value. Rather, I want the

WHERE query with IN on json\jsonb field

北战南征 提交于 2019-12-11 12:40:43
问题 I have field on some table (products), like "data". This field contains, for example, next data: [{"text" : "Text one"}, {"text" : "Text two"}, {"text" : "Text three"}] I need to be able find products, where json objects inside each object array on "data" field contain "text" : "Text one" or "text" : "Text two". Generally, I need to do IN query, but inside json "data" field -> array -> object. 回答1: Example data: create table products(id int, data json); insert into products values (1, '[{

Postgres jsonb index on nested integer field

感情迁移 提交于 2019-12-11 11:50:28
问题 I've got the following data structure in my postgres database - a jsonb column called customer { "RequestId": "00000000-0000-0000-0000-000000000000", "Customer": { "Status": "A", "AccountId": 14603582, "ProfileId": 172, "ReferralTypeId": 15 } "Contact": { "Telephone": "", "Email": "" } } I want to create an index on the ProfileId field, which is an integer. I've been unable to find an example of how to create an index on a nested field. The query I'm executing (which takes ~300s) is: select

Postgres - remove element from jsonb array

早过忘川 提交于 2019-12-11 10:39:59
问题 I have an array of jsonb elements ( jsonb[] ), with id and text. To remove an element I could use: UPDATE "Users" SET chats = array_remove(chats, '{"id": 2, "text": "my message"') But I want to delete the message just by the id, cause getting the message will cost me another query. 回答1: Assuming missing information: Your table has a PK called user_id . You want to remove all elements with id = 2 across the whole table. You don't want to touch other rows. id is unique within each array of

Execute Query based on the JSON that stored inside the column

此生再无相见时 提交于 2019-12-11 10:19:54
问题 I am using PostgresSQL database and I have four columns in a table and one of them stores a JSON, for example: {"a":"1","b":"2","c":"3"} I want to execute query that returns me all the rows which have c = 3 in that column. How can I achieve this with Hibernate? 回答1: Use ->> operator: -- Example data create table test (id int, js jsonb); insert into test values (1, '{"a":"1","b":"2","c":"3"}'), (2, '{"a":"1","b":"2","c":"4"}'); --query select * from test where js->>'c' = '3'; id | js ----+----